blob: d8649a08ce23088f1574b59a1a15a84ee8c5b11d [file] [log] [blame]
Randy Abernethyded64012015-02-15 11:28:40 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 /* jshint -W100 */
20
21/*
22 * JavaScript test suite for ThriftTest.thrift. These tests
23 * will run only with jQuery (-gen js:jquery) Apache Thrift
24 * interfaces. To create client code:
25 * $ thrift -gen js:jquery ThriftTest.thrift
26 *
27 * See also:
28 * ++ test.js for generic tests
29 * ++ test-nojq.js for "-gen js" only tests
30 */
31
32
33//////////////////////////////////
34//jQuery asynchronous tests
35jQuery.ajaxSetup({ timeout: 0 });
Kazuki Matsudab909a382016-02-13 19:36:09 +090036$(document).ajaxError(function() { QUnit.start(); });
Randy Abernethyded64012015-02-15 11:28:40 -080037
Kazuki Matsudab909a382016-02-13 19:36:09 +090038module('jQ Async Manual');
Randy Abernethyded64012015-02-15 11:28:40 -080039
Kazuki Matsudab909a382016-02-13 19:36:09 +090040 test('testI32', function() {
41 expect(2);
Randy Abernethyded64012015-02-15 11:28:40 -080042 QUnit.stop();
43
44 var transport = new Thrift.Transport();
Kazuki Matsudab909a382016-02-13 19:36:09 +090045 var protocol = new Thrift.Protocol(transport);
46 var client = new ThriftTest.ThriftTestClient(protocol);
Randy Abernethyded64012015-02-15 11:28:40 -080047
48 var jqxhr = jQuery.ajax({
Kazuki Matsudab909a382016-02-13 19:36:09 +090049 url: '/service',
50 data: client.send_testI32(Math.pow(-2, 31)),
51 type: 'POST',
Randy Abernethyded64012015-02-15 11:28:40 -080052 cache: false,
Kazuki Matsudab909a382016-02-13 19:36:09 +090053 dataType: 'text',
54 success: function(res) {
55 transport.setRecvBuffer(res);
56 equal(client.recv_testI32(), Math.pow(-2, 31));
Randy Abernethyded64012015-02-15 11:28:40 -080057 },
58 error: function() { ok(false); },
59 complete: function() {
60 ok(true);
61 QUnit.start();
62 }
63 });
64 });
65
Kazuki Matsudab909a382016-02-13 19:36:09 +090066 test('testI64', function() {
67 expect(2);
Randy Abernethyded64012015-02-15 11:28:40 -080068 QUnit.stop();
69
70 var transport = new Thrift.Transport();
Kazuki Matsudab909a382016-02-13 19:36:09 +090071 var protocol = new Thrift.Protocol(transport);
72 var client = new ThriftTest.ThriftTestClient(protocol);
Randy Abernethyded64012015-02-15 11:28:40 -080073
74 jQuery.ajax({
Kazuki Matsudab909a382016-02-13 19:36:09 +090075 url: '/service',
Randy Abernethyded64012015-02-15 11:28:40 -080076 //This is usually 2^61 but JS cannot represent anything over 2^52 accurately
Kazuki Matsudab909a382016-02-13 19:36:09 +090077 data: client.send_testI64(Math.pow(-2, 52)),
78 type: 'POST',
Randy Abernethyded64012015-02-15 11:28:40 -080079 cache: false,
Kazuki Matsudab909a382016-02-13 19:36:09 +090080 dataType: 'text',
81 success: function(res) {
82 transport.setRecvBuffer(res);
Randy Abernethyded64012015-02-15 11:28:40 -080083 //This is usually 2^61 but JS cannot represent anything over 2^52 accurately
Kazuki Matsudab909a382016-02-13 19:36:09 +090084 equal(client.recv_testI64(), Math.pow(-2, 52));
Randy Abernethyded64012015-02-15 11:28:40 -080085 },
86 error: function() { ok(false); },
87 complete: function() {
88 ok(true);
89 QUnit.start();
90 }
91 });
92 });
93
94
Kazuki Matsudab909a382016-02-13 19:36:09 +090095module('jQ Async');
96 test('I32', function() {
97 expect(3);
Randy Abernethyded64012015-02-15 11:28:40 -080098
99 QUnit.stop();
Kazuki Matsudab909a382016-02-13 19:36:09 +0900100 client.testI32(Math.pow(2, 30), function(result) {
101 equal(result, Math.pow(2, 30));
Randy Abernethyded64012015-02-15 11:28:40 -0800102 QUnit.start();
103 });
104
105 QUnit.stop();
Kazuki Matsudab909a382016-02-13 19:36:09 +0900106 var jqxhr = client.testI32(Math.pow(-2, 31), function(result) {
107 equal(result, Math.pow(-2, 31));
Randy Abernethyded64012015-02-15 11:28:40 -0800108 });
109
110 jqxhr.success(function(result) {
Kazuki Matsudab909a382016-02-13 19:36:09 +0900111 equal(result, Math.pow(-2, 31));
Randy Abernethyded64012015-02-15 11:28:40 -0800112 QUnit.start();
113 });
114 });
115
Kazuki Matsudab909a382016-02-13 19:36:09 +0900116 test('I64', function() {
117 expect(4);
Randy Abernethyded64012015-02-15 11:28:40 -0800118
119 QUnit.stop();
120 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
Kazuki Matsudab909a382016-02-13 19:36:09 +0900121 client.testI64(Math.pow(2, 52), function(result) {
122 equal(result, Math.pow(2, 52));
Randy Abernethyded64012015-02-15 11:28:40 -0800123 QUnit.start();
124 });
125
126 QUnit.stop();
127 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
Kazuki Matsudab909a382016-02-13 19:36:09 +0900128 client.testI64(Math.pow(-2, 52), function(result) {
129 equal(result, Math.pow(-2, 52));
Randy Abernethyded64012015-02-15 11:28:40 -0800130 })
Kazuki Matsudab909a382016-02-13 19:36:09 +0900131 .error(function(xhr, status, e) { ok(false, e.message); })
Randy Abernethyded64012015-02-15 11:28:40 -0800132 .success(function(result) {
133 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
Kazuki Matsudab909a382016-02-13 19:36:09 +0900134 equal(result, Math.pow(-2, 52));
Randy Abernethyded64012015-02-15 11:28:40 -0800135 })
136 .complete(function() {
137 ok(true);
138 QUnit.start();
139 });
140 });
141
Kazuki Matsudab909a382016-02-13 19:36:09 +0900142 test('Xception', function() {
143 expect(2);
Randy Abernethyded64012015-02-15 11:28:40 -0800144
145 QUnit.stop();
146
Kazuki Matsudab909a382016-02-13 19:36:09 +0900147 var dfd = client.testException('Xception', function(result) {
Randy Abernethyded64012015-02-15 11:28:40 -0800148 ok(false);
149 QUnit.start();
150 })
Kazuki Matsudab909a382016-02-13 19:36:09 +0900151 .error(function(xhr, status, e) {
Randy Abernethyded64012015-02-15 11:28:40 -0800152 equal(e.errorCode, 1001);
Kazuki Matsudab909a382016-02-13 19:36:09 +0900153 equal(e.message, 'Xception');
Randy Abernethyded64012015-02-15 11:28:40 -0800154 //QUnit.start();
155 //Note start is not required here because:
156 //$(document).ajaxError( function() { QUnit.start(); } );
157 });
158 });