Skip to content

Commit

Permalink
[azure-functions] add unit test for http request
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed Jan 10, 2024
1 parent 419d6b0 commit 40a0f4e
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 16 deletions.
5 changes: 3 additions & 2 deletions packages/azure-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"author": "Thada Wangthammang",
"license": "MIT",
"dependencies": {
"@nammatham/core": "2.0.0-alpha.8",
"@azure/functions": "^4.1.0",
"@nammatham/core": "2.0.0-alpha.8",
"colorette": "^2.0.20",
"express": "^4.18.2",
"undici": "5.20.0",
Expand All @@ -36,6 +36,7 @@
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/uuid": "^9.0.7"
"@types/uuid": "^9.0.7",
"node-mocks-http": "^1.14.1"
}
}
37 changes: 37 additions & 0 deletions packages/azure-functions/src/http/HttpRequest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { expect, test } from 'vitest';
import { HttpRequest } from './HttpRequest';
import httpMocks from 'node-mocks-http';
import exp from 'constants';

test('Test HttpRequest', () => {
// Arrange
const req = httpMocks.createRequest({
method: 'GET',
protocol: 'https',
// get: (key: string) => 'localhost',
url: '/api/test',
query: {
a: '1',
b: '2',
},
headers: {
x: '1',
y: '2',
},
});

// Act
const result = new HttpRequest(req);

// Assert
expect(result.method).toBe('GET');
expect(result.url).toBe('https://undefined/api/test');
expect(result.query.get('a')).toBe('1');
expect(result.query.get('b')).toBe('2');
expect(result.headers.get('x')).toBe('1');
expect(result.headers.get('y')).toBe('2');
expect(result.body).toBe(null);
expect(result.params).toStrictEqual({});
expect(result.user).toBe(null);
expect(result.bodyUsed).toBe(false);
});
6 changes: 3 additions & 3 deletions packages/azure-functions/src/http/HttpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import type * as types from '@azure/functions';
import type { HttpRequestParams, HttpRequestUser } from '@azure/functions';
import type { Blob } from 'buffer';
import type express from 'express';
import { logger } from '@nammatham/core';
import type { URLSearchParams } from 'url';
import type { ReadableStream } from 'stream/web';
import type { FormData, Headers } from 'undici';
import { logger } from '@nammatham/core';
import { Request as uRequest } from 'undici';
import type { URLSearchParams } from 'url';
import {
convertExpressQueryToURLSearchParams,
convertExpressReqHeaderToHeadersInit,
Expand All @@ -25,7 +25,7 @@ export class HttpRequest implements types.HttpRequest {
const url = getExpressReqFullUrl(req);
this.#body = req.body;
this.#uReq = new uRequest(url, {
body: this.#body,
body: req.method === 'GET' ? undefined : req.body,
method: req.method,
headers: convertExpressReqHeaderToHeadersInit(req.headers),
});
Expand Down
83 changes: 83 additions & 0 deletions packages/azure-functions/src/http/http-helper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { expect, test } from 'vitest';
import * as httpHelpers from './http-helpers';
import e, { Request as ExpressRequest } from 'express';

test('Test convertExpressQueryToURLSearchParams with string', () => {
// Arrange
const query = {
a: '1',
b: '2',
c: '3',
};

// Act
const result = httpHelpers.convertExpressQueryToURLSearchParams(query);

// Assert
expect(result).toBeInstanceOf(URLSearchParams);
expect(result.get('a')).toBe('1');
expect(result.get('b')).toBe('2');
expect(result.get('c')).toBe('3');
expect(result.toString()).toBe('a=1&b=2&c=3');
});

test('Test convertExpressQueryToURLSearchParams with array', () => {
// Arrange
const query = {
a: ['1', '2', '3'],
b: ['4', '5', '6'],
};

// Act
const result = httpHelpers.convertExpressQueryToURLSearchParams(query);

// Assert
expect(result).toBeInstanceOf(URLSearchParams);
expect(result.get('a')).toBe('1');
expect(result.get('b')).toBe('4');
expect(result.toString()).toBe('a=1&a=2&a=3&b=4&b=5&b=6');
});

test('Test convertExpressHeadersToHeaders with string', () => {
// Arrange
const headers = {
a: '1',
b: '2',
c: '3',
};

// Act
const result = httpHelpers.convertExpressReqHeaderToHeadersInit(headers);

// Assert
expect(result).toStrictEqual(headers);
});

test('Test convertExpressHeadersToHeaders with array', () => {
// Arrange
const headers = {
a: ['1', '2', '3'],
b: ['4', '5', '6'],
};

// Act
const result = httpHelpers.convertExpressReqHeaderToHeadersInit(headers);

// Assert
expect(result).toStrictEqual(headers);
});

test('Test getExpressReqFullUrl', () => {
// Arrange
const req = {
protocol: 'https',
get: () => 'localhost',
originalUrl: '/api/test',
} as any as ExpressRequest;

// Act
const result = httpHelpers.getExpressReqFullUrl(req);

// Assert
expect(result).toBe('https://localhost/api/test');
});
37 changes: 26 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 40a0f4e

Please sign in to comment.