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