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