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