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