Skip to content

Commit

Permalink
feat: Implementing lazy parsing for response headers and updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
klkucaj committed Dec 19, 2024
1 parent d877ede commit bd0d49d
Show file tree
Hide file tree
Showing 20 changed files with 614 additions and 183 deletions.
506 changes: 324 additions & 182 deletions lib/src/headers/headers.dart

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ void main() {
},
);

test(
'when a Access-Control-Allow-Credentials header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'access-control-allow-credentials': 'test'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a Access-Control-Allow-Credentials header with a value "true" '
'is passed then it should parse correctly',
Expand Down
15 changes: 15 additions & 0 deletions test/headers/basic/access_control_max_age_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ void main() {
},
);

test(
'when a Access-Control-Max-Age header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'access-control-max-age': 'test'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a Access-Control-Max-Age header is passed then it should parse the value correctly',
() async {
Expand Down
15 changes: 15 additions & 0 deletions test/headers/basic/allow_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ void main() {
},
);

test(
'when an Allow header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'allow': 'CUSTOM'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a valid Allow header is passed then it should parse the methods correctly',
() async {
Expand Down
15 changes: 15 additions & 0 deletions test/headers/basic/content_location_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ void main() {
},
);

test(
'when a Content-Location header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'content-location': 'https://example.com:test'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a Content-Location header with a valid URI is passed then it '
'should parse correctly',
Expand Down
15 changes: 15 additions & 0 deletions test/headers/basic/location_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ void main() {
},
);

test(
'when a Location header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'location': 'https://example.com:test'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a Location header with a valid URI is passed then it should parse correctly',
() async {
Expand Down
15 changes: 15 additions & 0 deletions test/headers/basic/trailer_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ void main() {
},
);

test(
'when a Trailer header with an empty value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'trailer': ''},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a valid Trailer header is passed then it should parse correctly',
() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ void main() {
},
);

test(
'when a Access-Control-Allow-Origin header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'access-control-allow-origin': 'https://example.com:test'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a Access-Control-Allow-Origin header with a valid URI origin is passed '
'then it should parse correctly',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ void main() {
},
);

test(
'when a Access-Control-Expose-Headers header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'access-control-expose-headers': '*, X-Custom-Header'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a Access-Control-Expose-Headers header with a single valid header is '
'passed then it should parse correctly',
Expand Down
17 changes: 17 additions & 0 deletions test/headers/typed_headers/cache_control_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ void main() {
},
);

test(
'when a Cache-Control header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {
'cache-control': 'max-age=3600, stale-while-revalidate=300'
},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a valid Cache-Control header is passed then it should parse the directives correctly',
() async {
Expand Down
15 changes: 15 additions & 0 deletions test/headers/typed_headers/content_disposition_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ void main() {
},
);

test(
'when a Content-Disposition header with an empty value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'content-disposition': ''},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a Content-Disposition header is passed then it should parse correctly',
() async {
Expand Down
15 changes: 15 additions & 0 deletions test/headers/typed_headers/content_encoding_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ void main() {
},
);

test(
'when a Content-Encoding header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'content-encoding': 'custom-encoding'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a single valid encoding is passed then it should parse correctly',
() async {
Expand Down
15 changes: 15 additions & 0 deletions test/headers/typed_headers/content_language_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ void main() {
},
);

test(
'when a Content-Language header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'content-language': 'en-123'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a single valid language is passed then it should parse correctly',
() async {
Expand Down
15 changes: 15 additions & 0 deletions test/headers/typed_headers/content_range_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ void main() {
},
);

test(
'when a Content-Range header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'content-range': 'bytes 500-499/1234'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a Content-Range header with a valid byte range is passed then it '
'should parse correctly',
Expand Down
15 changes: 15 additions & 0 deletions test/headers/typed_headers/etag_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ void main() {
},
);

test(
'when an ETag header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'etag': '123456'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a valid strong ETag is passed then it should parse correctly',
() async {
Expand Down
19 changes: 18 additions & 1 deletion test/headers/typed_headers/proxy_authenticate_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import 'package:relic/src/headers/headers.dart';
import 'package:relic/src/relic_server.dart';

import '../headers_test_utils.dart';
import '../docs/strict_validation_docs.dart';

/// Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authenticate
/// About empty value test, check the [StrictValidationDocs] class for more details.
void main() {
// Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authenticate
group('Given a Proxy-Authenticate header with the strict flag true', () {
late RelicServer server;

Expand Down Expand Up @@ -45,6 +47,21 @@ void main() {
},
);

test(
'when a Proxy-Authenticate header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'proxy-authenticate': 'Test'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

group('when Basic authentication', () {
test('with realm parameter should parse scheme correctly', () async {
Headers headers = await getServerRequestHeaders(
Expand Down
15 changes: 15 additions & 0 deletions test/headers/typed_headers/retry_after_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ void main() {
},
);

test(
'when a Retry-After header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'retry-after': 'invalid'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

group('when the header contains a delay in seconds', () {
test('then it should parse a valid positive integer correctly', () async {
Headers headers = await getServerRequestHeaders(
Expand Down
15 changes: 15 additions & 0 deletions test/headers/typed_headers/set_cookie_header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,21 @@ void main() {
},
);

test(
'when a Set-Cookie header with an invalid value is passed '
'then the server does not respond with a bad request if the headers '
'is not actually used',
() async {
Headers headers = await getServerRequestHeaders(
server: server,
headers: {'set-cookie': 'userId=42\x7F'},
echoHeaders: false,
);

expect(headers, isNotNull);
},
);

test(
'when a Set-Cookie header with an empty name is passed then it should parse the cookie correctly',
() async {
Expand Down
Loading

0 comments on commit bd0d49d

Please sign in to comment.