|
| 1 | +/* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| 2 | + |
| 3 | +import { Readable } from "stream"; |
| 4 | +import * as connectSdk from "../../src"; |
| 5 | +import * as communicator from "../../src/utils/communicator"; |
| 6 | + |
| 7 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 8 | +const config = require("../config.json"); |
| 9 | + |
| 10 | +connectSdk.init({ |
| 11 | + host: "httpbin.org", |
| 12 | + scheme: "http", |
| 13 | + port: 80, |
| 14 | + enableLogging: config.enableLogging, // defaults to false |
| 15 | + apiKeyId: config.apiKeyId, |
| 16 | + secretApiKey: config.secretApiKey, |
| 17 | + integrator: "Integration tests" |
| 18 | +}); |
| 19 | + |
| 20 | +jest.setTimeout(60 * 1000); |
| 21 | + |
| 22 | +function toReadable(str: string): Readable { |
| 23 | + const result = new Readable(); |
| 24 | + result.push(str); |
| 25 | + result.push(null); |
| 26 | + return result; |
| 27 | +} |
| 28 | + |
| 29 | +interface HttpBinResponse { |
| 30 | + form: { [key: string]: string | undefined }; |
| 31 | + files: { [key: string]: string | undefined }; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * @group integration |
| 36 | + */ |
| 37 | +describe("multipart", () => { |
| 38 | + test("POST", done => { |
| 39 | + const body = { |
| 40 | + value: "Hello World", |
| 41 | + file: { |
| 42 | + content: toReadable("This is the contents of a file"), |
| 43 | + fileName: "file.txt", |
| 44 | + contentType: "text/plain" |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + communicator.multipart({ |
| 49 | + method: "POST", |
| 50 | + modulePath: "/post", |
| 51 | + body: body, |
| 52 | + paymentContext: null, |
| 53 | + cb: (error, response) => { |
| 54 | + expect(error).toBeNull(); |
| 55 | + |
| 56 | + expect(response).not.toBeNull(); |
| 57 | + expect(response!.status).toBe(200); |
| 58 | + expect(response!.body).not.toBeNull(); |
| 59 | + |
| 60 | + const responseBody = response!.body as HttpBinResponse; |
| 61 | + expect(responseBody.files).not.toBeNull(); |
| 62 | + expect(responseBody.files.file).toBe("This is the contents of a file"); |
| 63 | + expect(responseBody.form).not.toBeNull(); |
| 64 | + expect(responseBody.form.value).toBe("Hello World"); |
| 65 | + |
| 66 | + done(); |
| 67 | + } |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + test("PUT", done => { |
| 72 | + const body = { |
| 73 | + value: "Hello World", |
| 74 | + file: { |
| 75 | + content: toReadable("This is the contents of a file"), |
| 76 | + fileName: "file.txt", |
| 77 | + contentType: "text/plain" |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + communicator.multipart({ |
| 82 | + method: "PUT", |
| 83 | + modulePath: "/put", |
| 84 | + body: body, |
| 85 | + paymentContext: null, |
| 86 | + cb: (error, response) => { |
| 87 | + expect(error).toBeNull(); |
| 88 | + |
| 89 | + expect(response).not.toBeNull(); |
| 90 | + expect(response!.status).toBe(200); |
| 91 | + expect(response!.body).not.toBeNull(); |
| 92 | + |
| 93 | + const responseBody = response!.body as HttpBinResponse; |
| 94 | + expect(responseBody.files).not.toBeNull(); |
| 95 | + expect(responseBody.files.file).toBe("This is the contents of a file"); |
| 96 | + expect(responseBody.form).not.toBeNull(); |
| 97 | + expect(responseBody.form.value).toBe("Hello World"); |
| 98 | + |
| 99 | + done(); |
| 100 | + } |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments