Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [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 | var thrift = require('thrift'); |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 20 | var assert = require('assert'); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 21 | |
| 22 | var ThriftTest = require('./gen-nodejs/ThriftTest'), |
| 23 | ttypes = require('./gen-nodejs/ThriftTest_types'); |
| 24 | |
| 25 | var connection = thrift.createConnection('localhost', 9090), |
| 26 | client = thrift.createClient(ThriftTest, connection); |
| 27 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 28 | connection.on('error', function(err) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 29 | assert(false, err); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 30 | }); |
| 31 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 32 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 33 | |
| 34 | client.testVoid(function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 35 | assert( ! err); |
| 36 | assert.equal(undefined, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 37 | }); |
| 38 | |
| 39 | client.testString("Test", function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 40 | assert( ! err); |
| 41 | assert.equal("Test", response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 42 | }); |
| 43 | |
| 44 | client.testByte(1, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 45 | assert( ! err); |
| 46 | assert.equal(1, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 47 | }); |
| 48 | |
| 49 | client.testI32(-1, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 50 | assert( ! err); |
| 51 | assert.equal(-1, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 52 | }); |
| 53 | |
| 54 | client.testI64(5, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 55 | assert( ! err); |
| 56 | assert.equal(5, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 57 | }); |
| 58 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 59 | client.testI64(-5, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 60 | assert( ! err); |
| 61 | assert.equal(-5, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 62 | }); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 63 | |
| 64 | client.testI64(-34359738368, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 65 | assert( ! err); |
| 66 | assert.equal(-34359738368, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 67 | }); |
| 68 | |
| 69 | client.testDouble(-5.2098523, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 70 | assert( ! err); |
| 71 | assert.equal(-5.2098523, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 72 | }); |
| 73 | |
| 74 | var out = new ttypes.Xtruct({ |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 75 | string_thing: 'Zero', |
| 76 | byte_thing: 1, |
| 77 | i32_thing: -3, |
| 78 | //i64_thing: 1000000 |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 79 | }); |
| 80 | client.testStruct(out, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 81 | assert( ! err); |
| 82 | assert.deepEqual(out, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 83 | }); |
| 84 | |
| 85 | var out2 = new ttypes.Xtruct2(); |
| 86 | out2.byte_thing = 1; |
| 87 | out2.struct_thing = out; |
| 88 | out2.i32_thing = 5; |
| 89 | client.testNest(out2, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 90 | assert( ! err); |
| 91 | assert.deepEqual(out2, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 92 | }); |
| 93 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 94 | var mapout = {}; |
| 95 | for (var i = 0; i < 5; ++i) { |
| 96 | mapout[i] = i-10; |
| 97 | } |
| 98 | client.testMap(mapout, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 99 | assert( ! err); |
| 100 | assert.deepEqual(mapout, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 101 | }); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * TODO: testSet, testList, testEnum, testTypedef, testMapMap, testInsanity |
| 105 | */ |
| 106 | |
| 107 | |
| 108 | client.testException('ApplicationException', function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 109 | //assert.equal('ApplicationException', err); |
| 110 | assert( ! response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 111 | }); |
| 112 | |
| 113 | client.testException('Xception', function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 114 | assert.equal('Xception', err.message); |
| 115 | assert( ! response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 116 | }); |
| 117 | |
| 118 | client.testException('success', function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 119 | assert( ! err); |
| 120 | //assert.equal('success', response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 121 | }); |
| 122 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 123 | |
| 124 | setTimeout(function() { |
| 125 | console.log("Server successfully tested!"); |
| 126 | connection.end(); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 127 | }, 200); |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame^] | 128 | |
| 129 | // to make it also run on expresso |
| 130 | exports.expressoTest = function() {}; |
| 131 | |