blob: f62bb957b181b1af0f1944c909582b3f7cef2bf3 [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 });
Randy Abernethyded64012015-02-15 11:28:40 -080036
Brian Forbisb5d6ea32018-08-25 23:39:29 -040037QUnit.module('jQ Async Manual');
Randy Abernethyded64012015-02-15 11:28:40 -080038
Brian Forbisb5d6ea32018-08-25 23:39:29 -040039 QUnit.test('testI32', function(assert) {
40 assert.expect(2);
41 const done = assert.async(2);
Randy Abernethyded64012015-02-15 11:28:40 -080042
Brian Forbisb5d6ea32018-08-25 23:39:29 -040043 const transport = new Thrift.Transport();
44 const protocol = new Thrift.Protocol(transport);
45 const client = new ThriftTest.ThriftTestClient(protocol);
Randy Abernethyded64012015-02-15 11:28:40 -080046
Brian Forbisb5d6ea32018-08-25 23:39:29 -040047 const jqxhr = jQuery.ajax({
Kazuki Matsudab909a382016-02-13 19:36:09 +090048 url: '/service',
49 data: client.send_testI32(Math.pow(-2, 31)),
50 type: 'POST',
Randy Abernethyded64012015-02-15 11:28:40 -080051 cache: false,
Kazuki Matsudab909a382016-02-13 19:36:09 +090052 dataType: 'text',
53 success: function(res) {
54 transport.setRecvBuffer(res);
Brian Forbisb5d6ea32018-08-25 23:39:29 -040055 assert.equal(client.recv_testI32(), Math.pow(-2, 31));
56 done();
Randy Abernethyded64012015-02-15 11:28:40 -080057 },
Brian Forbisb5d6ea32018-08-25 23:39:29 -040058 error: function() { assert.ok(false); },
Randy Abernethyded64012015-02-15 11:28:40 -080059 complete: function() {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040060 assert.ok(true);
61 done();
Randy Abernethyded64012015-02-15 11:28:40 -080062 }
63 });
64 });
65
Brian Forbisb5d6ea32018-08-25 23:39:29 -040066 QUnit.test('testI64', function(assert) {
67 assert.expect(2);
68 const done = assert.async(2);
Randy Abernethyded64012015-02-15 11:28:40 -080069
Brian Forbisb5d6ea32018-08-25 23:39:29 -040070 const transport = new Thrift.Transport();
71 const protocol = new Thrift.Protocol(transport);
72 const 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
Brian Forbisb5d6ea32018-08-25 23:39:29 -040084 assert.equal(client.recv_testI64(), Math.pow(-2, 52));
85 done();
Randy Abernethyded64012015-02-15 11:28:40 -080086 },
Brian Forbisb5d6ea32018-08-25 23:39:29 -040087 error: function() { assert.ok(false); },
Randy Abernethyded64012015-02-15 11:28:40 -080088 complete: function() {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040089 assert.ok(true);
90 done();
Randy Abernethyded64012015-02-15 11:28:40 -080091 }
92 });
93 });
94
95
Brian Forbisb5d6ea32018-08-25 23:39:29 -040096QUnit.module('jQ Async');
97 QUnit.test('I32', function(assert) {
98 assert.expect(3);
Randy Abernethyded64012015-02-15 11:28:40 -080099
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400100 const done = assert.async(3);
Kazuki Matsudab909a382016-02-13 19:36:09 +0900101 client.testI32(Math.pow(2, 30), function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400102 assert.equal(result, Math.pow(2, 30));
103 done();
Randy Abernethyded64012015-02-15 11:28:40 -0800104 });
105
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400106 const jqxhr = client.testI32(Math.pow(-2, 31), function(result) {
107 assert.equal(result, Math.pow(-2, 31));
108 done();
Randy Abernethyded64012015-02-15 11:28:40 -0800109 });
110
111 jqxhr.success(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400112 assert.equal(result, Math.pow(-2, 31));
113 done();
Randy Abernethyded64012015-02-15 11:28:40 -0800114 });
115 });
116
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400117 QUnit.test('I64', function(assert) {
118 assert.expect(4);
Randy Abernethyded64012015-02-15 11:28:40 -0800119
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400120 const done = assert.async(4);
Randy Abernethyded64012015-02-15 11:28:40 -0800121 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
Kazuki Matsudab909a382016-02-13 19:36:09 +0900122 client.testI64(Math.pow(2, 52), function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400123 assert.equal(result, Math.pow(2, 52));
124 done();
Randy Abernethyded64012015-02-15 11:28:40 -0800125 });
126
Randy Abernethyded64012015-02-15 11:28:40 -0800127 //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) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400129 assert.equal(result, Math.pow(-2, 52));
130 done();
Randy Abernethyded64012015-02-15 11:28:40 -0800131 })
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400132 .error(function(xhr, status, e) { assert.ok(false, e.message); })
Randy Abernethyded64012015-02-15 11:28:40 -0800133 .success(function(result) {
134 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400135 assert.equal(result, Math.pow(-2, 52));
136 done();
Randy Abernethyded64012015-02-15 11:28:40 -0800137 })
138 .complete(function() {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400139 assert.ok(true);
140 done();
Randy Abernethyded64012015-02-15 11:28:40 -0800141 });
142 });
143
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400144 QUnit.test('Xception', function(assert) {
145 assert.expect(2);
Randy Abernethyded64012015-02-15 11:28:40 -0800146
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400147 const done = assert.async(2);
Randy Abernethyded64012015-02-15 11:28:40 -0800148
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400149 const dfd = client.testException('Xception', function(result) {
150 assert.ok(false);
151 done();
Randy Abernethyded64012015-02-15 11:28:40 -0800152 })
Kazuki Matsudab909a382016-02-13 19:36:09 +0900153 .error(function(xhr, status, e) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400154 assert.equal(e.errorCode, 1001);
155 assert.equal(e.message, 'Xception');
156 done();
157 $(document).ajaxError( function() { done(); } );
Randy Abernethyded64012015-02-15 11:28:40 -0800158 });
159 });