-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[azure-functions] add unit test for http request
- Loading branch information
1 parent
419d6b0
commit 40a0f4e
Showing
5 changed files
with
152 additions
and
16 deletions.
There are no files selected for viewing
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
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,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); | ||
}); |
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
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,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'); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.