microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
test/extension/networkInspector/requestBodyFormatter.test.ts
70lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import { OutputChannelLogger } from "../../../src/extension/log/OutputChannelLogger"; |
| 5 | import { LogLevel } from "../../../src/extension/log/LogHelper"; |
| 6 | import { RequestBodyFormatter } from "../../../src/extension/networkInspector/requestBodyFormatters/requestBodyFormatter"; |
| 7 | import { Response, Request } from "../../../src/extension/networkInspector/networkMessageData"; |
| 8 | import * as sinon from "sinon"; |
| 9 | import assert = require("assert"); |
| 10 | |
| 11 | suite("requestBodyFormatter", function () { |
| 12 | suite("formatBody", function () { |
| 13 | let testLogger = OutputChannelLogger.getChannel("Network Inspector test"); |
| 14 | let loggedOutput: Array<string> = []; |
| 15 | sinon.stub(testLogger, "log", function (message: string, level: LogLevel) { |
| 16 | loggedOutput.push(message); |
| 17 | }); |
| 18 | const requestBodyFormatter = new RequestBodyFormatter(testLogger); |
| 19 | |
| 20 | setup(() => { |
| 21 | loggedOutput = []; |
| 22 | }); |
| 23 | |
| 24 | test("should return text data and print warnings via logger", () => { |
| 25 | const response: Response = { |
| 26 | headers: [ |
| 27 | { key: "Content-Encoding", value: "gzip" }, |
| 28 | { key: "Content-Type", value: "application/json;charset=utf-8" }, |
| 29 | ], |
| 30 | data: "H4sIAAAAAAAA/6tWSkksSVSyUihJLS6pBQAeHWwqDgAAAA==", |
| 31 | id: "6826df34-173d-4351-a7e5-d435328f2e54", |
| 32 | index: 0, |
| 33 | isMock: false, |
| 34 | status: 200, |
| 35 | timestamp: 1617959201269, |
| 36 | totalChunks: 1, |
| 37 | reason: "", |
| 38 | insights: null, |
| 39 | }; |
| 40 | |
| 41 | const formattedBodyRes = requestBodyFormatter.formatBody(response); |
| 42 | assert.strictEqual(loggedOutput[0].includes("GraphQLFormatter"), true); |
| 43 | assert.strictEqual(loggedOutput[1].includes("JSONFormatter"), true); |
| 44 | assert.strictEqual(formattedBodyRes, '{"data": test}'); |
| 45 | }); |
| 46 | |
| 47 | test("should parse JSON objects and return an array of objects", () => { |
| 48 | const request: Request = { |
| 49 | headers: [ |
| 50 | { key: "Accept-Encoding", value: "gzip" }, |
| 51 | { key: "Content-Type", value: "application/json;charset=utf-8" }, |
| 52 | ], |
| 53 | data: "eyJkYXRhU3RyIjogInRlc3QifQp7ImRhdGFOdW0iOiAxMjN9CnsiZGF0YUFyciI6IFsxLCAyXX0=", |
| 54 | id: "6826df34-173d-4351-a7e5-d435328f2e54", |
| 55 | timestamp: 1617959201269, |
| 56 | url: "https://test.org", |
| 57 | method: "POST", |
| 58 | }; |
| 59 | |
| 60 | const formattedBodyReference = [ |
| 61 | { dataStr: "test" }, |
| 62 | { dataNum: 123 }, |
| 63 | { dataArr: [1, 2] }, |
| 64 | ]; |
| 65 | |
| 66 | const formattedBodyRes = requestBodyFormatter.formatBody(request); |
| 67 | assert.deepStrictEqual(formattedBodyRes, formattedBodyReference); |
| 68 | }); |
| 69 | }); |
| 70 | }); |
| 71 | |