Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
export const EOL = '\r\n'
export const EOL2X = EOL + EOL

const BASIC_LATIN = '[\\u0009\\u0020-\\u007E]'
const HTTP_METHODS = '(CONNECT|OPTIONS|TRACE|GET|HEAD|POST|PUT|PATCH|DELETE)'
const HTTP_PROTOCOL_VERSIONS = '(HTTP)\\/(1\\.0|1\\.1|2(\\.0){0,1})'
const HTTP_PROTOCOL_VERSIONS = '(HTTP)\\/(1\\.0|1\\.1|2(\\.0){0,1}|3(\\.0){0,1})'

export const regexps = {
quote: /"/g,
nlStart: new RegExp(`^${EOL}`),
nlEnd: new RegExp(`${EOL}$`),
requestStartRow: new RegExp(`^${HTTP_METHODS}\\s\\S*\\s${HTTP_PROTOCOL_VERSIONS}$`),
responseStartRow: new RegExp(`^${HTTP_PROTOCOL_VERSIONS}\\s\\d{3}\\s${BASIC_LATIN}*$`),
responseStartRow: new RegExp(`^${HTTP_PROTOCOL_VERSIONS}\\s\\d{3}\\s[^\r\n]*$`),
// eslint-disable-next-line no-control-regex
quotedHeaderValue: new RegExp('^"[\\u0009\\u0020\\u0021\\u0023-\\u007E]+"$'),
boundary: new RegExp(`(?<=boundary=)"{0,1}[A-Za-z0-9'()+_,.:=?-]+"{0,1}`),
Expand All @@ -33,7 +32,8 @@ export enum HttpProtocol {
export enum HttpProtocolVersion {
http10 = 'HTTP/1.0',
http11 = 'HTTP/1.1',
http20 = 'HTTP/2.0',
http2 = 'HTTP/2',
http3 = 'HTTP/3',
}

export enum HttpMethod {
Expand Down
15 changes: 12 additions & 3 deletions test/parsers/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,19 @@ describe('parsers / request', () => {
test(startRow, expected)
})

it('should parse valid startRow when HTTP protocol is v2.0', () => {
const startRow = 'GET /features HTTP/2.0'
it('should parse valid startRow when HTTP protocol is v2', () => {
const startRow = 'GET /features HTTP/2'
const expected = getExpected({
protocolVersion: HttpProtocolVersion.http20,
protocolVersion: HttpProtocolVersion.http2,
})

test(startRow, expected)
})

it('should parse valid startRow when HTTP protocol is v3', () => {
const startRow = 'GET /features HTTP/3'
const expected = getExpected({
protocolVersion: HttpProtocolVersion.http3,
})

test(startRow, expected)
Expand Down
8 changes: 4 additions & 4 deletions test/parsers/response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ describe('parsers / response', () => {

it('should set instance fields when startRow has valid format (reason is not empty)', () => {
const parser = getParserInstance()
parser['startRow'] = 'HTTP/1.1 201 Created'
parser['startRow'] = 'HTTP/3 500 Server Error'

parser['_parseStartRow']()
expect(parser['protocolVersion']).toEqual('HTTP/1.1')
expect(parser['statusCode']).toEqual(201)
expect(parser['statusMessage']).toEqual('Created')
expect(parser['protocolVersion']).toEqual('HTTP/3')
expect(parser['statusCode']).toEqual(500)
expect(parser['statusMessage']).toEqual('Server Error')
})
})

Expand Down