Skip to content

Commit ef32cc9

Browse files
committed
feat: Unit tests and response parsing logic
1 parent 0fd67dc commit ef32cc9

File tree

12 files changed

+136
-11
lines changed

12 files changed

+136
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.vscode/
2+
reports/

deno.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
},
3434
"lint": {
3535
"plugins": [
36-
"jsr:@loat-dev/[email protected].6/colon_spacing",
37-
"jsr:@loat-dev/[email protected].6/imports",
38-
"jsr:@loat-dev/[email protected].6/arrays"
36+
"jsr:@loat-dev/[email protected].11/colon_spacing",
37+
"jsr:@loat-dev/[email protected].11/imports",
38+
"jsr:@loat-dev/[email protected].11/arrays"
3939
],
4040
"rules": {
4141
"tags": [

deno.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/http/error/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { InvalidStartLineError } from './invalid_start_line_error.ts'
2+
export { InvalidResponseError } from './invalid_response_error.ts'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class InvalidResponseError extends Error {
2+
constructor() {
3+
super('Invalid response. Did not find header end.');
4+
}
5+
}

src/http/response/body/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { parseChunkedBody } from './parse_chunked_body.ts'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function parseChunkedBody(body : string) : string {
2+
return body
3+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { assertEquals } from '@std/assert'
2+
import { parseHeaders } from './parse_headers.ts';
3+
4+
Deno.test('Tests "parseHeaders"', async (test) => {
5+
await test.step({
6+
name: 'Parse valid HTTP headers.',
7+
fn() : void {
8+
const responseContent = 'key-1: value1\r\nkey-2: value 2 with space'
9+
assertEquals(
10+
Object.fromEntries(parseHeaders(responseContent)),
11+
{
12+
'key-1': 'value1',
13+
'key-2': 'value 2 with space'
14+
}
15+
)
16+
}
17+
})
18+
19+
await test.step({
20+
name: 'Parse (valid) HTTP headers. With leading/trailing newline.',
21+
fn() : void {
22+
const responseContent = '\r\nkey-1: value1\r\nkey-2: value 2 with space\r\n'
23+
assertEquals(
24+
Object.fromEntries(parseHeaders(responseContent)),
25+
{
26+
'key-1': 'value1',
27+
'key-2': 'value 2 with space'
28+
}
29+
)
30+
}
31+
})
32+
33+
await test.step({
34+
name: 'Parse (valid) HTTP headers. With space in key name.',
35+
fn() : void {
36+
const responseContent = 'key 1: value 1 with space'
37+
assertEquals(
38+
Object.fromEntries(parseHeaders(responseContent)),
39+
{'key 1': 'value 1 with space'}
40+
)
41+
}
42+
})
43+
})
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
/**
2-
* This function parses the HTTP response, and returns the headers
2+
* This function parses the HTTP response, and returns the headers.
3+
*
4+
* @param response Response string
5+
* @returns The headers as `<key>: <value>` map.
36
*/
4-
export function parseHeaders(response : string) : Record<string, string> {
5-
return {}
7+
export function parseHeaders(response : string) : Map<string, string> {
8+
const headers = new Map<string, string>();
9+
response.trim().split('\r\n').forEach((line) => {
10+
const [key, value] = line.split(':');
11+
headers.set(key, value.trim());
12+
})
13+
14+
return headers;
615
}

src/http/response/parse.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
import { parseStartLine } from './start_line/index.ts';
12
import { parseHeaders } from './headers/index.ts';
3+
import { parseChunkedBody } from './body/index.ts';
24
import { splitResponse } from './split_response.ts';
3-
import { parseStartLine } from './start_line/index.ts';
45

56
export function parse(response : string) : Response {
67
const splitResult = splitResponse(response)
78

89
const startLine = parseStartLine(splitResult.startLine);
910
const headers = parseHeaders(splitResult.headers);
10-
const body = splitResult.body;
11+
let body = splitResult.body;
12+
13+
if (headers.get('Transfer-Encoding') === 'chunked') {
14+
body = parseChunkedBody(body);
15+
}
1116

1217
return new Response(
1318
body,

0 commit comments

Comments
 (0)