David Mai | ddd2224 | 2019-02-22 03:37:49 -0800 | [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 | |
| 20 | const TFramedTransport = require("../lib/thrift/framed_transport"); |
| 21 | const THeaderTransport = require("../lib/thrift/header_transport"); |
| 22 | const THeaderProtocol = require("../lib/thrift/header_protocol"); |
| 23 | const thrift = require("../lib/thrift"); |
| 24 | const fs = require("fs"); |
| 25 | const test = require("tape"); |
| 26 | const path = require("path"); |
| 27 | |
| 28 | const headerPayload = fs.readFileSync( |
| 29 | path.join(__dirname, "test_header_payload") |
| 30 | ); |
| 31 | |
| 32 | const cases = { |
| 33 | "Should read headers from payload": function(assert) { |
| 34 | const transport = new TFramedTransport(); |
| 35 | transport.inBuf = Buffer.from(headerPayload); |
| 36 | |
| 37 | const headers = transport.readHeaders(); |
| 38 | assert.equals(headers.Parent, "shoobar"); |
| 39 | assert.equals(headers.Trace, "abcde"); |
| 40 | assert.end(); |
| 41 | }, |
| 42 | "Should read headers when reading message begin": function(assert) { |
| 43 | const transport = new TFramedTransport(); |
| 44 | transport.inBuf = Buffer.from(headerPayload); |
| 45 | const protocol = new THeaderProtocol(transport); |
| 46 | const result = protocol.readMessageBegin(); |
| 47 | |
| 48 | const headers = transport.getReadHeaders(); |
| 49 | assert.equals(headers.Parent, "shoobar"); |
| 50 | assert.equals(headers.Trace, "abcde"); |
| 51 | assert.equals(result.fname, "add"); |
| 52 | assert.equals(result.mtype, thrift.Thrift.MessageType.CALL); |
| 53 | assert.end(); |
| 54 | }, |
| 55 | "Should be able to write headers": function(assert) { |
| 56 | const writeTransport = new TFramedTransport(); |
| 57 | writeTransport.setProtocolId(THeaderTransport.SubprotocolId.BINARY); |
| 58 | writeTransport.setWriteHeader("Hihihihi", "hohohoho"); |
| 59 | writeTransport.setWriteHeader("boobooboo", "fooshoopoo"); |
| 60 | writeTransport.setWriteHeader("a", "z"); |
| 61 | writeTransport.writeHeaders(); |
| 62 | const writeBuffer = writeTransport.outBuffers[0]; |
| 63 | |
| 64 | const readTransport = new TFramedTransport(); |
| 65 | readTransport.inBuf = writeBuffer; |
| 66 | readTransport.readHeaders(); |
| 67 | |
| 68 | const headers = readTransport.getReadHeaders(); |
| 69 | assert.equals(headers.Hihihihi, "hohohoho"); |
| 70 | assert.equals(headers.boobooboo, "fooshoopoo"); |
| 71 | assert.equals(headers.a, "z"); |
| 72 | assert.end(); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | Object.keys(cases).forEach(function(caseName) { |
| 77 | test(caseName, cases[caseName]); |
| 78 | }); |