Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 1 | /* |
| 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 |
| 35 | jQuery.ajaxSetup({ timeout: 0 }); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 36 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 37 | QUnit.module('jQ Async Manual'); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 38 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 39 | QUnit.test('testI32', function(assert) { |
| 40 | assert.expect(2); |
| 41 | const done = assert.async(2); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 42 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 43 | const transport = new Thrift.Transport(); |
| 44 | const protocol = new Thrift.Protocol(transport); |
| 45 | const client = new ThriftTest.ThriftTestClient(protocol); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 46 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 47 | const jqxhr = jQuery.ajax({ |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 48 | url: '/service', |
| 49 | data: client.send_testI32(Math.pow(-2, 31)), |
| 50 | type: 'POST', |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 51 | cache: false, |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 52 | dataType: 'text', |
| 53 | success: function(res) { |
| 54 | transport.setRecvBuffer(res); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 55 | assert.equal(client.recv_testI32(), Math.pow(-2, 31)); |
| 56 | done(); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 57 | }, |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 58 | error: function() { assert.ok(false); }, |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 59 | complete: function() { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 60 | assert.ok(true); |
| 61 | done(); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 62 | } |
| 63 | }); |
| 64 | }); |
| 65 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 66 | QUnit.test('testI64', function(assert) { |
| 67 | assert.expect(2); |
| 68 | const done = assert.async(2); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 69 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 70 | const transport = new Thrift.Transport(); |
| 71 | const protocol = new Thrift.Protocol(transport); |
| 72 | const client = new ThriftTest.ThriftTestClient(protocol); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 73 | |
| 74 | jQuery.ajax({ |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 75 | url: '/service', |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 76 | //This is usually 2^61 but JS cannot represent anything over 2^52 accurately |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 77 | data: client.send_testI64(Math.pow(-2, 52)), |
| 78 | type: 'POST', |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 79 | cache: false, |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 80 | dataType: 'text', |
| 81 | success: function(res) { |
| 82 | transport.setRecvBuffer(res); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 83 | //This is usually 2^61 but JS cannot represent anything over 2^52 accurately |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 84 | assert.equal(client.recv_testI64(), Math.pow(-2, 52)); |
| 85 | done(); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 86 | }, |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 87 | error: function() { assert.ok(false); }, |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 88 | complete: function() { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 89 | assert.ok(true); |
| 90 | done(); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 91 | } |
| 92 | }); |
| 93 | }); |
| 94 | |
| 95 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 96 | QUnit.module('jQ Async'); |
| 97 | QUnit.test('I32', function(assert) { |
| 98 | assert.expect(3); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 99 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 100 | const done = assert.async(3); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 101 | client.testI32(Math.pow(2, 30), function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 102 | assert.equal(result, Math.pow(2, 30)); |
| 103 | done(); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 104 | }); |
| 105 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 106 | const jqxhr = client.testI32(Math.pow(-2, 31), function(result) { |
| 107 | assert.equal(result, Math.pow(-2, 31)); |
| 108 | done(); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 109 | }); |
| 110 | |
| 111 | jqxhr.success(function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 112 | assert.equal(result, Math.pow(-2, 31)); |
| 113 | done(); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 114 | }); |
| 115 | }); |
| 116 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 117 | QUnit.test('I64', function(assert) { |
| 118 | assert.expect(4); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 119 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 120 | const done = assert.async(4); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 121 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 122 | client.testI64(Math.pow(2, 52), function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 123 | assert.equal(result, Math.pow(2, 52)); |
| 124 | done(); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 125 | }); |
| 126 | |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 127 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 128 | client.testI64(Math.pow(-2, 52), function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 129 | assert.equal(result, Math.pow(-2, 52)); |
| 130 | done(); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 131 | }) |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 132 | .error(function(xhr, status, e) { assert.ok(false, e.message); }) |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 133 | .success(function(result) { |
| 134 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 135 | assert.equal(result, Math.pow(-2, 52)); |
| 136 | done(); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 137 | }) |
| 138 | .complete(function() { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 139 | assert.ok(true); |
| 140 | done(); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 141 | }); |
| 142 | }); |
| 143 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 144 | QUnit.test('Xception', function(assert) { |
| 145 | assert.expect(2); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 146 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 147 | const done = assert.async(2); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 148 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 149 | const dfd = client.testException('Xception', function(result) { |
| 150 | assert.ok(false); |
| 151 | done(); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 152 | }) |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 153 | .error(function(xhr, status, e) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 154 | assert.equal(e.errorCode, 1001); |
| 155 | assert.equal(e.message, 'Xception'); |
| 156 | done(); |
| 157 | $(document).ajaxError( function() { done(); } ); |
Randy Abernethy | ded6401 | 2015-02-15 11:28:40 -0800 | [diff] [blame] | 158 | }); |
| 159 | }); |