blob: 3a90cac7f3adff715b025d7aff6f36488ad2037a [file] [log] [blame]
Roger Meiereaa61d82012-01-12 21:38:29 +00001/*
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 */
19var thrift = require('thrift');
Henrique99be0272013-05-10 23:43:12 +020020var assert = require('assert');
Roger Meiereaa61d82012-01-12 21:38:29 +000021
22var ThriftTest = require('./gen-nodejs/ThriftTest'),
23 ttypes = require('./gen-nodejs/ThriftTest_types');
24
25var connection = thrift.createConnection('localhost', 9090),
26 client = thrift.createClient(ThriftTest, connection);
27
Roger Meiereaa61d82012-01-12 21:38:29 +000028connection.on('error', function(err) {
Henrique99be0272013-05-10 23:43:12 +020029 assert(false, err);
Roger Meiereaa61d82012-01-12 21:38:29 +000030});
31
Henrique99be0272013-05-10 23:43:12 +020032
Roger Meiereaa61d82012-01-12 21:38:29 +000033
34client.testVoid(function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020035 assert( ! err);
36 assert.equal(undefined, response);
Roger Meiereaa61d82012-01-12 21:38:29 +000037});
38
39client.testString("Test", function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020040 assert( ! err);
41 assert.equal("Test", response);
Roger Meiereaa61d82012-01-12 21:38:29 +000042});
43
44client.testByte(1, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020045 assert( ! err);
46 assert.equal(1, response);
Roger Meiereaa61d82012-01-12 21:38:29 +000047});
48
49client.testI32(-1, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020050 assert( ! err);
51 assert.equal(-1, response);
Roger Meiereaa61d82012-01-12 21:38:29 +000052});
53
54client.testI64(5, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020055 assert( ! err);
56 assert.equal(5, response);
Roger Meiereaa61d82012-01-12 21:38:29 +000057});
58
Roger Meiereaa61d82012-01-12 21:38:29 +000059client.testI64(-5, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020060 assert( ! err);
61 assert.equal(-5, response);
Roger Meiereaa61d82012-01-12 21:38:29 +000062});
Roger Meiereaa61d82012-01-12 21:38:29 +000063
64client.testI64(-34359738368, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020065 assert( ! err);
66 assert.equal(-34359738368, response);
Roger Meiereaa61d82012-01-12 21:38:29 +000067});
68
69client.testDouble(-5.2098523, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020070 assert( ! err);
71 assert.equal(-5.2098523, response);
Roger Meiereaa61d82012-01-12 21:38:29 +000072});
73
74var out = new ttypes.Xtruct({
Henrique99be0272013-05-10 23:43:12 +020075 string_thing: 'Zero',
76 byte_thing: 1,
77 i32_thing: -3,
78 //i64_thing: 1000000
Roger Meiereaa61d82012-01-12 21:38:29 +000079});
80client.testStruct(out, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020081 assert( ! err);
82 assert.deepEqual(out, response);
Roger Meiereaa61d82012-01-12 21:38:29 +000083});
84
85var out2 = new ttypes.Xtruct2();
86out2.byte_thing = 1;
87out2.struct_thing = out;
88out2.i32_thing = 5;
89client.testNest(out2, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020090 assert( ! err);
91 assert.deepEqual(out2, response);
Roger Meiereaa61d82012-01-12 21:38:29 +000092});
93
Roger Meiereaa61d82012-01-12 21:38:29 +000094var mapout = {};
95for (var i = 0; i < 5; ++i) {
96 mapout[i] = i-10;
97}
98client.testMap(mapout, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020099 assert( ! err);
100 assert.deepEqual(mapout, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000101});
Roger Meiereaa61d82012-01-12 21:38:29 +0000102
103/*
104 * TODO: testSet, testList, testEnum, testTypedef, testMapMap, testInsanity
105 */
106
107
108client.testException('ApplicationException', function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200109 //assert.equal('ApplicationException', err);
110 assert( ! response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000111});
112
113client.testException('Xception', function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200114 assert.equal('Xception', err.message);
115 assert( ! response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000116});
117
118client.testException('success', function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200119 assert( ! err);
120 //assert.equal('success', response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000121});
122
Henrique99be0272013-05-10 23:43:12 +0200123
124setTimeout(function() {
125 console.log("Server successfully tested!");
126 connection.end();
Roger Meiereaa61d82012-01-12 21:38:29 +0000127}, 200);
Henrique99be0272013-05-10 23:43:12 +0200128
129// to make it also run on expresso
130exports.expressoTest = function() {};
131