mirror of https://github.com/grpc/grpc-node.git
Add fromHttp2Headers
This commit is contained in:
parent
95827662b4
commit
fb38744985
|
@ -179,4 +179,33 @@ export class Metadata {
|
|||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new Metadata object based fields in a given IncomingHttpHeaders
|
||||
* object.
|
||||
* @param headers An IncomingHttpHeaders object.
|
||||
*/
|
||||
static fromHttp2Headers(headers: http2.IncomingHttpHeaders): Metadata {
|
||||
const result = new Metadata();
|
||||
forOwn(headers, (values, key) => {
|
||||
if (isBinaryKey(key)) {
|
||||
if (Array.isArray(values)) {
|
||||
values.forEach((value) => {
|
||||
result.add(key, Buffer.from(value, 'base64'));
|
||||
})
|
||||
} else {
|
||||
result.add(key, Buffer.from(values, 'base64'));
|
||||
}
|
||||
} else {
|
||||
if (Array.isArray(values)) {
|
||||
values.forEach((value) => {
|
||||
result.add(key, value);
|
||||
})
|
||||
} else {
|
||||
result.add(key, values);
|
||||
}
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import * as assert from 'assert';
|
||||
import * as http2 from 'http2';
|
||||
import { range } from 'lodash';
|
||||
import * as metadata from '../src/metadata';
|
||||
|
||||
|
@ -6,6 +7,13 @@ class Metadata extends metadata.Metadata {
|
|||
getInternalRepresentation() {
|
||||
return this.internalRepr;
|
||||
}
|
||||
|
||||
static fromHttp2Headers(headers: http2.IncomingHttpHeaders): Metadata {
|
||||
const result = metadata.Metadata.fromHttp2Headers(headers) as Metadata;
|
||||
result.getInternalRepresentation =
|
||||
Metadata.prototype.getInternalRepresentation;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
const validKeyChars = '0123456789abcdefghijklmnopqrstuvwxyz_-.';
|
||||
|
@ -224,7 +232,7 @@ describe('Metadata', () => {
|
|||
});
|
||||
|
||||
describe('toHttp2Headers', () => {
|
||||
it('creates an http2.OutgoingHttpHeaders object', () => {
|
||||
it('creates an OutgoingHttpHeaders object with expected values', () => {
|
||||
metadata.add('key1', 'value1');
|
||||
metadata.add('Key2', 'value2');
|
||||
metadata.add('KEY3', 'value3a');
|
||||
|
@ -249,4 +257,37 @@ describe('Metadata', () => {
|
|||
assert.deepEqual(metadata.toHttp2Headers(), {});
|
||||
});
|
||||
});
|
||||
|
||||
describe('fromHttp2Headers', () => {
|
||||
it('creates a Metadata object with expected values', () => {
|
||||
const headers = {
|
||||
key1: 'value1',
|
||||
key2: ['value2'],
|
||||
key3: ['value3a', 'value3b'],
|
||||
'key-bin': [
|
||||
'AAECAwQFBgcICQoLDA0ODw==',
|
||||
'EBESExQVFhcYGRobHB0eHw==',
|
||||
'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8='
|
||||
]
|
||||
};
|
||||
const metadataFromHeaders = Metadata.fromHttp2Headers(headers);
|
||||
const internalRepr = metadataFromHeaders.getInternalRepresentation();
|
||||
assert.deepEqual(internalRepr, {
|
||||
key1: ['value1'],
|
||||
key2: ['value2'],
|
||||
key3: ['value3a', 'value3b'],
|
||||
'key-bin': [
|
||||
Buffer.from(range(0, 16)),
|
||||
Buffer.from(range(16, 32)),
|
||||
Buffer.from(range(0, 32))
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
it('creates an empty Metadata object from empty headers', () => {
|
||||
const metadataFromHeaders = Metadata.fromHttp2Headers({});
|
||||
const internalRepr = metadataFromHeaders.getInternalRepresentation();
|
||||
assert.deepEqual(internalRepr, {});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue