| Index: test/request_test.dart
|
| diff --git a/test/request_test.dart b/test/request_test.dart
|
| index fa75bd0b9cf72c5a667ed51d080418beb511efa9..4afeff4568837e70738d6ad7a09cb38375715870 100644
|
| --- a/test/request_test.dart
|
| +++ b/test/request_test.dart
|
| @@ -29,94 +29,145 @@ void main() {
|
| expect(request.protocolVersion, '1.0');
|
| });
|
|
|
| - test('requestedUri must be absolute', () {
|
| - expect(() => new Request('GET', Uri.parse('/path')), throwsArgumentError);
|
| - });
|
| -
|
| - test('if uri is null, scriptName must be null', () {
|
| - expect(() => new Request('GET', Uri.parse('/path'),
|
| - scriptName: '/script/name'), throwsArgumentError);
|
| - });
|
| -
|
| - test('if scriptName is null, uri must be null', () {
|
| - var relativeUri = new Uri(path: '/cool/beans.html');
|
| - expect(() => new Request('GET', Uri.parse('/path'), url: relativeUri),
|
| - throwsArgumentError);
|
| - });
|
| -
|
| - test('uri must be relative', () {
|
| - var relativeUri = Uri.parse('http://localhost/test');
|
| -
|
| - expect(() => new Request('GET', LOCALHOST_URI,
|
| - url: relativeUri, scriptName: '/news'), throwsArgumentError);
|
| -
|
| - // NOTE: explicitly testing fragments due to Issue 18053
|
| - relativeUri = Uri.parse('http://localhost/test#fragment');
|
| -
|
| - expect(() => new Request('GET', LOCALHOST_URI,
|
| - url: relativeUri, scriptName: '/news'), throwsArgumentError);
|
| - });
|
| + group("url", () {
|
| + test("defaults to the requestedUri's relativized path and query", () {
|
| + var request =
|
| + new Request('GET', Uri.parse("http://localhost/foo/bar?q=1"));
|
| + expect(request.url, equals(Uri.parse("foo/bar?q=1")));
|
| + });
|
|
|
| - test('uri and scriptName', () {
|
| - var pathInfo = '/pages/page.html?utm_source=ABC123';
|
| - var scriptName = '/assets/static';
|
| - var fullUrl = 'http://localhost/other_path/other_resource.asp';
|
| - var request = new Request('GET', Uri.parse(fullUrl),
|
| - url: Uri.parse(pathInfo), scriptName: scriptName);
|
| + test("is inferred from handlerPath if possible", () {
|
| + var request = new Request(
|
| + 'GET', Uri.parse("http://localhost/foo/bar?q=1"),
|
| + handlerPath: '/foo/');
|
| + expect(request.url, equals(Uri.parse("bar?q=1")));
|
| + });
|
|
|
| - expect(request.scriptName, scriptName);
|
| - expect(request.url.path, '/pages/page.html');
|
| - expect(request.url.query, 'utm_source=ABC123');
|
| + test("uses the given value if passed", () {
|
| + var request = new Request(
|
| + 'GET', Uri.parse("http://localhost/foo/bar?q=1"),
|
| + url: Uri.parse("bar?q=1"));
|
| + expect(request.url, equals(Uri.parse("bar?q=1")));
|
| + });
|
| });
|
|
|
| - test('minimal uri', () {
|
| - var pathInfo = '/';
|
| - var scriptName = '/assets/static';
|
| - var fullUrl = 'http://localhost$scriptName$pathInfo';
|
| - var request = new Request('GET', Uri.parse(fullUrl),
|
| - url: Uri.parse(pathInfo), scriptName: scriptName);
|
| + group("handlerPath", () {
|
| + test("defaults to '/'", () {
|
| + var request = new Request('GET', Uri.parse("http://localhost/foo/bar"));
|
| + expect(request.handlerPath, equals('/'));
|
| + });
|
|
|
| - expect(request.scriptName, scriptName);
|
| - expect(request.url.path, '/');
|
| - expect(request.url.query, '');
|
| - });
|
| + test("is inferred from url if possible", () {
|
| + var request = new Request(
|
| + 'GET', Uri.parse("http://localhost/foo/bar?q=1"),
|
| + url: Uri.parse("bar?q=1"));
|
| + expect(request.handlerPath, equals("/foo/"));
|
| + });
|
|
|
| - test('invalid url', () {
|
| - var testUrl = 'page';
|
| - var scriptName = '/assets/static';
|
| - var fullUrl = 'http://localhost$scriptName$testUrl';
|
| + test("uses the given value if passed", () {
|
| + var request = new Request(
|
| + 'GET', Uri.parse("http://localhost/foo/bar?q=1"),
|
| + handlerPath: '/foo/');
|
| + expect(request.handlerPath, equals("/foo/"));
|
| + });
|
|
|
| - expect(() => new Request('GET', Uri.parse(fullUrl),
|
| - url: Uri.parse(testUrl), scriptName: scriptName),
|
| - throwsArgumentError);
|
| + test("adds a trailing slash to the given value if necessary", () {
|
| + var request = new Request(
|
| + 'GET', Uri.parse("http://localhost/foo/bar?q=1"),
|
| + handlerPath: '/foo');
|
| + expect(request.handlerPath, equals("/foo/"));
|
| + expect(request.url, equals(Uri.parse("bar?q=1")));
|
| + });
|
| });
|
|
|
| - test('scriptName with no leading slash', () {
|
| - var pathInfo = '/page';
|
| - var scriptName = 'assets/static';
|
| - var fullUrl = 'http://localhost/assets/static/pages';
|
| -
|
| - expect(() => new Request('GET', Uri.parse(fullUrl),
|
| - url: Uri.parse(pathInfo), scriptName: scriptName),
|
| - throwsArgumentError);
|
| -
|
| - pathInfo = '/assets/static/page';
|
| - scriptName = '/';
|
| - fullUrl = 'http://localhost/assets/static/pages';
|
| + group("errors", () {
|
| + group('requestedUri', () {
|
| + test('must be absolute', () {
|
| + expect(() => new Request('GET', Uri.parse('/path')),
|
| + throwsArgumentError);
|
| + });
|
| +
|
| + test('may not have a fragment', () {
|
| + expect(() {
|
| + new Request('GET', Uri.parse('http://localhost/#fragment'));
|
| + }, throwsArgumentError);
|
| + });
|
| + });
|
|
|
| - expect(() => new Request('GET', Uri.parse(fullUrl),
|
| - url: Uri.parse(pathInfo), scriptName: scriptName),
|
| - throwsArgumentError);
|
| - });
|
| + group('url', () {
|
| + test('must be relative', () {
|
| + expect(() {
|
| + new Request('GET', Uri.parse('http://localhost/test'),
|
| + url: Uri.parse('http://localhost/test'));
|
| + }, throwsArgumentError);
|
| + });
|
| +
|
| + test('may not be root-relative', () {
|
| + expect(() {
|
| + new Request('GET', Uri.parse('http://localhost/test'),
|
| + url: Uri.parse('/test'));
|
| + }, throwsArgumentError);
|
| + });
|
| +
|
| + test('may not have a fragment', () {
|
| + expect(() {
|
| + new Request('GET', Uri.parse('http://localhost/test'),
|
| + url: Uri.parse('test#fragment'));
|
| + }, throwsArgumentError);
|
| + });
|
| +
|
| + test('must be a suffix of requestedUri', () {
|
| + expect(() {
|
| + new Request('GET', Uri.parse('http://localhost/dir/test'),
|
| + url: Uri.parse('dir'));
|
| + }, throwsArgumentError);
|
| + });
|
| +
|
| + test('must have the same query parameters as requestedUri', () {
|
| + expect(() {
|
| + new Request('GET', Uri.parse('http://localhost/test?q=1&r=2'),
|
| + url: Uri.parse('test?q=2&r=1'));
|
| + }, throwsArgumentError);
|
| +
|
| + // Order matters for query parameters.
|
| + expect(() {
|
| + new Request('GET', Uri.parse('http://localhost/test?q=1&r=2'),
|
| + url: Uri.parse('test?r=2&q=1'));
|
| + }, throwsArgumentError);
|
| + });
|
| + });
|
|
|
| - test('scriptName that is only a slash', () {
|
| - var pathInfo = '/assets/static/page';
|
| - var scriptName = '/';
|
| - var fullUrl = 'http://localhost/assets/static/pages';
|
| + group('handlerPath', () {
|
| + test('must be a prefix of requestedUri', () {
|
| + expect(() {
|
| + new Request('GET', Uri.parse('http://localhost/dir/test'),
|
| + handlerPath: '/test');
|
| + }, throwsArgumentError);
|
| + });
|
| +
|
| + test('must start with "/"', () {
|
| + expect(() {
|
| + new Request('GET', Uri.parse('http://localhost/test'),
|
| + handlerPath: 'test');
|
| + }, throwsArgumentError);
|
| + });
|
| + });
|
|
|
| - expect(() => new Request('GET', Uri.parse(fullUrl),
|
| - url: Uri.parse(pathInfo), scriptName: scriptName),
|
| - throwsArgumentError);
|
| + group('handlerPath + url must', () {
|
| + test('be requestedUrl path', () {
|
| + expect(() {
|
| + new Request('GET', Uri.parse('http://localhost/foo/bar/baz'),
|
| + handlerPath: '/foo/', url: Uri.parse('baz'));
|
| + }, throwsArgumentError);
|
| + });
|
| +
|
| + test('be on a path boundary', () {
|
| + expect(() {
|
| + new Request('GET', Uri.parse('http://localhost/foo/bar/baz'),
|
| + handlerPath: '/foo/ba', url: Uri.parse('r/baz'));
|
| + }, throwsArgumentError);
|
| + });
|
| + });
|
| });
|
| });
|
|
|
| @@ -143,8 +194,8 @@ void main() {
|
| var request = new Request('GET', uri,
|
| protocolVersion: '2.0',
|
| headers: {'header1': 'header value 1'},
|
| - url: Uri.parse('/file.html'),
|
| - scriptName: '/static',
|
| + url: Uri.parse('file.html'),
|
| + handlerPath: '/static/',
|
| body: controller.stream,
|
| context: {'context1': 'context value 1'});
|
|
|
| @@ -155,7 +206,7 @@ void main() {
|
| expect(copy.protocolVersion, request.protocolVersion);
|
| expect(copy.headers, same(request.headers));
|
| expect(copy.url, request.url);
|
| - expect(copy.scriptName, request.scriptName);
|
| + expect(copy.handlerPath, request.handlerPath);
|
| expect(copy.context, same(request.context));
|
| expect(copy.readAsString(), completion('hello, world'));
|
|
|
| @@ -167,41 +218,42 @@ void main() {
|
| });
|
| });
|
|
|
| - group('with just scriptName', () {
|
| - test('updates url if scriptName matches existing url', () {
|
| - var uri = Uri.parse('https://test.example.com/static/file.html');
|
| - var request = new Request('GET', uri);
|
| - var copy = request.change(scriptName: '/static');
|
| + group('with path', () {
|
| + test('updates handlerPath and url', () {
|
| + var uri = Uri.parse('https://test.example.com/static/dir/file.html');
|
| + var request = new Request('GET', uri,
|
| + handlerPath: '/static/', url: Uri.parse('dir/file.html'));
|
| + var copy = request.change(path: 'dir');
|
|
|
| - expect(copy.scriptName, '/static');
|
| - expect(copy.url, Uri.parse('/file.html'));
|
| + expect(copy.handlerPath, '/static/dir/');
|
| + expect(copy.url, Uri.parse('file.html'));
|
| });
|
|
|
| - test('throws if striptName does not match existing url', () {
|
| - var uri = Uri.parse('https://test.example.com/static/file.html');
|
| - var request = new Request('GET', uri);
|
| + test('allows a trailing slash', () {
|
| + var uri = Uri.parse('https://test.example.com/static/dir/file.html');
|
| + var request = new Request('GET', uri,
|
| + handlerPath: '/static/', url: Uri.parse('dir/file.html'));
|
| + var copy = request.change(path: 'dir/');
|
|
|
| - expect(() => request.change(scriptName: '/nope'), throwsArgumentError);
|
| + expect(copy.handlerPath, '/static/dir/');
|
| + expect(copy.url, Uri.parse('file.html'));
|
| });
|
| - });
|
|
|
| - test('url', () {
|
| - var uri = Uri.parse('https://test.example.com/static/file.html');
|
| - var request = new Request('GET', uri);
|
| - var copy = request.change(url: Uri.parse('/other_path/file.html'));
|
| + test('throws if path does not match existing uri', () {
|
| + var uri = Uri.parse('https://test.example.com/static/dir/file.html');
|
| + var request = new Request('GET', uri,
|
| + handlerPath: '/static/', url: Uri.parse('dir/file.html'));
|
|
|
| - expect(copy.scriptName, '');
|
| - expect(copy.url, Uri.parse('/other_path/file.html'));
|
| - });
|
| + expect(() => request.change(path: 'wrong'), throwsArgumentError);
|
| + });
|
|
|
| - test('scriptName and url', () {
|
| - var uri = Uri.parse('https://test.example.com/static/file.html');
|
| - var request = new Request('GET', uri);
|
| - var copy = request.change(
|
| - scriptName: '/dynamic', url: Uri.parse('/other_path/file.html'));
|
| + test("throws if path isn't a path boundary", () {
|
| + var uri = Uri.parse('https://test.example.com/static/dir/file.html');
|
| + var request = new Request('GET', uri,
|
| + handlerPath: '/static/', url: Uri.parse('dir/file.html'));
|
|
|
| - expect(copy.scriptName, '/dynamic');
|
| - expect(copy.url, Uri.parse('/other_path/file.html'));
|
| + expect(() => request.change(path: 'di'), throwsArgumentError);
|
| + });
|
| });
|
| });
|
| }
|
|
|