-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'test/composite-metadata-tests' into 1.0.x-alpha
- Loading branch information
Showing
4 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
.../rsocket-composite-metadata/__tests__/__snapshots__/encodeWellKnownMetadataHeader.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`encodeWellKnownMetadataHeader encodes the header as per spec 1`] = ` | ||
Array [ | ||
133, | ||
0, | ||
0, | ||
16, | ||
] | ||
`; |
44 changes: 44 additions & 0 deletions
44
packages/rsocket-composite-metadata/__tests__/encodeAndAddWellKnownMetadata.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
encodeAndAddWellKnownMetadata, | ||
WellKnownMimeType, | ||
} from "rsocket-composite-metadata"; | ||
import { readUInt24BE } from "rsocket-core"; | ||
|
||
describe("encodeWellKnownMetadataHeader", () => { | ||
it("encodes the header as per spec when WellKnownMimeType given", () => { | ||
const metadata = encodeAndAddWellKnownMetadata( | ||
Buffer.from([]), | ||
WellKnownMimeType.MESSAGE_RSOCKET_MIMETYPE, | ||
Buffer.from("test") | ||
); | ||
|
||
// 122 | 128 | ||
const maskedId = metadata.readUInt8(0); | ||
const length = readUInt24BE(metadata, 1); | ||
const value = metadata.slice(4, metadata.length); | ||
|
||
expect(maskedId).toBe(250); | ||
expect(length).toBe(4); | ||
expect(value.length).toBe(4); | ||
expect(value.toString("utf-8")).toBe("test"); | ||
}); | ||
|
||
it("encodes the header as per spec when identifier given", () => { | ||
const metadata = encodeAndAddWellKnownMetadata( | ||
Buffer.from([]), | ||
// MESSAGE_RSOCKET_MIMETYPE | ||
122, | ||
Buffer.from("test") | ||
); | ||
|
||
// 122 | 128 | ||
const maskedId = metadata.readUInt8(0); | ||
const length = readUInt24BE(metadata, 1); | ||
const value = metadata.slice(4, metadata.length); | ||
|
||
expect(maskedId).toBe(250); | ||
expect(length).toBe(4); | ||
expect(value.length).toBe(4); | ||
expect(value.toString("utf-8")).toBe("test"); | ||
}); | ||
}); |
152 changes: 152 additions & 0 deletions
152
packages/rsocket-composite-metadata/__tests__/encodeCompositeMetadata.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { | ||
encodeCompositeMetadata, | ||
WellKnownMimeType, | ||
} from "rsocket-composite-metadata"; | ||
import { readUInt24BE } from "rsocket-core"; | ||
|
||
describe("encodeCompositeMetadata encodes the metadata", () => { | ||
describe("when given a map", () => { | ||
it("handles WellKnownMimeType instances as keys", () => { | ||
const metadata = new Map(); | ||
metadata.set( | ||
WellKnownMimeType.MESSAGE_RSOCKET_MIMETYPE, | ||
Buffer.from("test") | ||
); | ||
|
||
const encoded = encodeCompositeMetadata(metadata); | ||
|
||
// 122 | 128 | ||
const maskedId = encoded.readUInt8(0); | ||
const length = readUInt24BE(encoded, 1); | ||
const value = encoded.slice(4, encoded.length); | ||
|
||
expect(maskedId).toBe(250); | ||
expect(length).toBe(4); | ||
expect(value.length).toBe(4); | ||
expect(value.toString("utf-8")).toBe("test"); | ||
}); | ||
|
||
it("handles WellKnownMimeType identifiers as keys", () => { | ||
const metadata = new Map(); | ||
metadata.set(122, Buffer.from("test")); | ||
|
||
const encoded = encodeCompositeMetadata(metadata); | ||
|
||
// 122 | 128 | ||
const maskedId = encoded.readUInt8(0); | ||
const length = readUInt24BE(encoded, 1); | ||
const value = encoded.slice(4, encoded.length); | ||
|
||
expect(maskedId).toBe(250); | ||
expect(length).toBe(4); | ||
expect(value.length).toBe(4); | ||
expect(value.toString("utf-8")).toBe("test"); | ||
}); | ||
|
||
it("handles custom mimetypes as keys", () => { | ||
const metadata = new Map(); | ||
metadata.set("custom", Buffer.from("test")); | ||
|
||
const encoded = encodeCompositeMetadata(metadata); | ||
|
||
const mimeTypeLengthMinusOne = encoded.readUInt8(0); | ||
const start = 1; | ||
const end = mimeTypeLengthMinusOne + 2; | ||
const mimeType = encoded.slice(start, end); | ||
const metadataLength = readUInt24BE(encoded, mimeTypeLengthMinusOne + 2); | ||
const metadataValue = encoded.slice(encoded.length - metadataLength); | ||
|
||
expect(mimeTypeLengthMinusOne).toBe(5); | ||
expect(mimeType.toString("utf-8")).toBe("custom"); | ||
expect(metadataLength).toBe(4); | ||
expect(metadataValue.toString("utf-8")).toBe("test"); | ||
}); | ||
|
||
it("handles mimetype value as function", () => { | ||
const metadata = new Map(); | ||
metadata.set("custom", () => Buffer.from("test")); | ||
|
||
const encoded = encodeCompositeMetadata(metadata); | ||
|
||
const mimeTypeLengthMinusOne = encoded.readUInt8(0); | ||
const start = 1; | ||
const end = mimeTypeLengthMinusOne + 2; | ||
const mimeType = encoded.slice(start, end); | ||
const metadataLength = readUInt24BE(encoded, mimeTypeLengthMinusOne + 2); | ||
const metadataValue = encoded.slice(encoded.length - metadataLength); | ||
|
||
expect(mimeTypeLengthMinusOne).toBe(5); | ||
expect(mimeType.toString("utf-8")).toBe("custom"); | ||
expect(metadataLength).toBe(4); | ||
expect(metadataValue.toString("utf-8")).toBe("test"); | ||
}); | ||
}); | ||
|
||
describe("when given a array", () => { | ||
it("handles WellKnownMimeType instances as keys", () => { | ||
const encoded = encodeCompositeMetadata([ | ||
[WellKnownMimeType.MESSAGE_RSOCKET_MIMETYPE, Buffer.from("test")], | ||
]); | ||
|
||
// 122 | 128 | ||
const maskedId = encoded.readUInt8(0); | ||
const length = readUInt24BE(encoded, 1); | ||
const value = encoded.slice(4, encoded.length); | ||
|
||
expect(maskedId).toBe(250); | ||
expect(length).toBe(4); | ||
expect(value.length).toBe(4); | ||
expect(value.toString("utf-8")).toBe("test"); | ||
}); | ||
|
||
it("handles WellKnownMimeType identifiers as keys", () => { | ||
const encoded = encodeCompositeMetadata([[122, Buffer.from("test")]]); | ||
|
||
// 122 | 128 | ||
const maskedId = encoded.readUInt8(0); | ||
const length = readUInt24BE(encoded, 1); | ||
const value = encoded.slice(4, encoded.length); | ||
|
||
expect(maskedId).toBe(250); | ||
expect(length).toBe(4); | ||
expect(value.length).toBe(4); | ||
expect(value.toString("utf-8")).toBe("test"); | ||
}); | ||
|
||
it("handles custom mimetypes as keys", () => { | ||
const encoded = encodeCompositeMetadata([ | ||
["custom", Buffer.from("test")], | ||
]); | ||
|
||
const mimeTypeLengthMinusOne = encoded.readUInt8(0); | ||
const start = 1; | ||
const end = mimeTypeLengthMinusOne + 2; | ||
const mimeType = encoded.slice(start, end); | ||
const metadataLength = readUInt24BE(encoded, mimeTypeLengthMinusOne + 2); | ||
const metadataValue = encoded.slice(encoded.length - metadataLength); | ||
|
||
expect(mimeTypeLengthMinusOne).toBe(5); | ||
expect(mimeType.toString("utf-8")).toBe("custom"); | ||
expect(metadataLength).toBe(4); | ||
expect(metadataValue.toString("utf-8")).toBe("test"); | ||
}); | ||
|
||
it("handles mimetype value as function", () => { | ||
const encoded = encodeCompositeMetadata([ | ||
["custom", () => Buffer.from("test")], | ||
]); | ||
|
||
const mimeTypeLengthMinusOne = encoded.readUInt8(0); | ||
const start = 1; | ||
const end = mimeTypeLengthMinusOne + 2; | ||
const mimeType = encoded.slice(start, end); | ||
const metadataLength = readUInt24BE(encoded, mimeTypeLengthMinusOne + 2); | ||
const metadataValue = encoded.slice(encoded.length - metadataLength); | ||
|
||
expect(mimeTypeLengthMinusOne).toBe(5); | ||
expect(mimeType.toString("utf-8")).toBe("custom"); | ||
expect(metadataLength).toBe(4); | ||
expect(metadataValue.toString("utf-8")).toBe("test"); | ||
}); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
packages/rsocket-composite-metadata/__tests__/encodeWellKnownMetadataHeader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { | ||
encodeWellKnownMetadataHeader, | ||
WellKnownMimeType, | ||
} from "rsocket-composite-metadata"; | ||
|
||
describe("encodeWellKnownMetadataHeader", () => { | ||
it("encodes the header as per spec", () => { | ||
const header = encodeWellKnownMetadataHeader( | ||
WellKnownMimeType.APPLICATION_JSON.identifier, | ||
WellKnownMimeType.APPLICATION_JSON.toString().length | ||
); | ||
const actual = header.toJSON().data; | ||
expect(actual).toMatchSnapshot(); | ||
}); | ||
}); |