blob: 99bb832bc3c80c7cb57788ebb17fd8a57c06b5f9 [file] [log] [blame]
David Maiddd22242019-02-22 03:37:49 -08001/*
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
20const TFramedTransport = require("../lib/thrift/framed_transport");
21const THeaderTransport = require("../lib/thrift/header_transport");
22const THeaderProtocol = require("../lib/thrift/header_protocol");
23const thrift = require("../lib/thrift");
24const fs = require("fs");
25const test = require("tape");
26const path = require("path");
27
28const headerPayload = fs.readFileSync(
29 path.join(__dirname, "test_header_payload")
30);
31
32const 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 },
Nick Gavalasdd53b942023-05-11 17:13:13 -050042 "Should read different headers from different payload": function(assert) {
43 const transport = new TFramedTransport();
44 const buf = Buffer.from(headerPayload);
45 buf[24] = 115; // Change Parent to Parens
46 buf[32] = 122; // Change shoobar to shoobaz
47 transport.inBuf = buf;
48
49 const headers = transport.readHeaders();
50 assert.equals(headers.Parent, undefined);
51 assert.equals(headers.Parens, "shoobaz");
52 assert.equals(headers.Trace, "abcde");
53 assert.end();
54 },
David Maiddd22242019-02-22 03:37:49 -080055 "Should read headers when reading message begin": function(assert) {
56 const transport = new TFramedTransport();
57 transport.inBuf = Buffer.from(headerPayload);
58 const protocol = new THeaderProtocol(transport);
59 const result = protocol.readMessageBegin();
60
61 const headers = transport.getReadHeaders();
62 assert.equals(headers.Parent, "shoobar");
63 assert.equals(headers.Trace, "abcde");
64 assert.equals(result.fname, "add");
65 assert.equals(result.mtype, thrift.Thrift.MessageType.CALL);
66 assert.end();
67 },
68 "Should be able to write headers": function(assert) {
69 const writeTransport = new TFramedTransport();
70 writeTransport.setProtocolId(THeaderTransport.SubprotocolId.BINARY);
71 writeTransport.setWriteHeader("Hihihihi", "hohohoho");
72 writeTransport.setWriteHeader("boobooboo", "fooshoopoo");
73 writeTransport.setWriteHeader("a", "z");
74 writeTransport.writeHeaders();
75 const writeBuffer = writeTransport.outBuffers[0];
76
77 const readTransport = new TFramedTransport();
78 readTransport.inBuf = writeBuffer;
79 readTransport.readHeaders();
80
81 const headers = readTransport.getReadHeaders();
82 assert.equals(headers.Hihihihi, "hohohoho");
83 assert.equals(headers.boobooboo, "fooshoopoo");
84 assert.equals(headers.a, "z");
85 assert.end();
Nick Gavalasdd53b942023-05-11 17:13:13 -050086 },
87 "Separate transports should have separate headers": function(assert) {
88 const writeTransport = new TFramedTransport();
89 writeTransport.setProtocolId(THeaderTransport.SubprotocolId.BINARY);
90 writeTransport.setWriteHeader("foo", "bar");
91 const headers = writeTransport.getWriteHeaders();
92
93 const otherWriteTransport = new TFramedTransport();
94 otherWriteTransport.setProtocolId(THeaderTransport.SubprotocolId.BINARY);
95 otherWriteTransport.setWriteHeader("otherfoo", "baz");
96 const otherHeaders = otherWriteTransport.getWriteHeaders();
97
98 assert.equals(headers.foo, "bar");
99 assert.equals(headers.otherfoo, undefined);
100 assert.equals(otherHeaders.foo, undefined);
101 assert.equals(otherHeaders.otherfoo, "baz");
102 assert.end();
David Maiddd22242019-02-22 03:37:49 -0800103 }
104};
105
106Object.keys(cases).forEach(function(caseName) {
107 test(caseName, cases[caseName]);
108});