Chromium Code Reviews| Index: lib/src/http_basic_test.dart |
| diff --git a/lib/src/http_basic_test.dart b/lib/src/http_basic_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9959756aaf6cef7a3321a6b42413cc26850c987d |
| --- /dev/null |
| +++ b/lib/src/http_basic_test.dart |
| @@ -0,0 +1,294 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
|
bkonyi
2017/04/19 16:37:52
I'll change the copyright date.
zra
2017/04/19 17:46:13
tests should go under the test/ directory.
bkonyi
2017/04/19 21:29:50
Done.
bkonyi
2017/04/19 21:29:50
Done.
|
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import "dart:isolate"; |
| +import "dart:io"; |
| +import "../sync_http.dart"; |
|
zra
2017/04/19 17:46:13
This should be a package: import, like:
import "p
bkonyi
2017/04/19 21:29:50
Tried this before, didn't realize I needed to run
|
| +import "../../../sdk/pkg/expect/lib/expect.dart"; |
|
bkonyi
2017/04/19 16:37:52
This needs to be changed to not use an explicit pa
zra
2017/04/19 17:46:13
Use the "test" package. Add it under "dev_dependen
bkonyi
2017/04/19 21:29:50
Done.
|
| + |
| +class TestServerMain { |
| + TestServerMain() |
| + : _statusPort = new ReceivePort(), |
| + _serverPort = null; |
|
zra
2017/04/19 17:46:13
not needed
bkonyi
2017/04/19 21:29:50
Removed.
|
| + |
| + void setServerStartedHandler(void startedCallback(int port)) { |
| + _startedCallback = startedCallback; |
| + } |
| + |
| + void start([bool chunkedEncoding = false]) { |
| + ReceivePort receivePort = new ReceivePort(); |
| + var remote = Isolate.spawn(startTestServer, receivePort.sendPort); |
|
zra
2017/04/19 17:46:13
remote is unused
bkonyi
2017/04/19 21:29:50
Removed.
|
| + receivePort.first.then((port) { |
| + _serverPort = port; |
| + |
| + if (chunkedEncoding) { |
| + // Send chunked encoding message to the server. |
| + port.send( |
| + [new TestServerCommand.chunkedEncoding(), _statusPort.sendPort]); |
| + } |
| + |
| + // Send server start message to the server. |
| + var command = new TestServerCommand.start(); |
| + port.send([command, _statusPort.sendPort]); |
| + }); |
| + |
| + // Handle status messages from the server. |
| + _statusPort.listen((var status) { |
| + if (status.isStarted) { |
| + _startedCallback(status.port); |
| + } |
| + }); |
| + } |
| + |
| + void close() { |
| + // Send server stop message to the server. |
| + _serverPort.send([new TestServerCommand.stop(), _statusPort.sendPort]); |
| + _statusPort.close(); |
| + } |
| + |
| + ReceivePort _statusPort; // Port for receiving messages from the server. |
|
zra
2017/04/19 17:46:13
I think Dart style tends towards putting these at
bkonyi
2017/04/19 21:29:50
Good point. Moved.
|
| + SendPort _serverPort; // Port for sending messages to the server. |
| + var _startedCallback; |
|
zra
2017/04/19 17:46:13
You can make a typedef for the function type and u
bkonyi
2017/04/19 21:29:50
Done.
|
| +} |
| + |
| +class TestServerCommand { |
|
zra
2017/04/19 17:46:13
How about an enum?
bkonyi
2017/04/19 21:29:50
Done.
|
| + static const START = 0; |
|
zra
2017/04/19 17:46:13
Check the Dart style guide fro naming constants. A
bkonyi
2017/04/19 21:29:50
Replaced with enum.
|
| + static const STOP = 1; |
| + static const CHUNKED_ENCODING = 2; |
| + |
| + TestServerCommand.start() : _command = START; |
| + TestServerCommand.stop() : _command = STOP; |
| + TestServerCommand.chunkedEncoding() : _command = CHUNKED_ENCODING; |
| + |
| + bool get isStart => _command == START; |
| + bool get isStop => _command == STOP; |
| + bool get isChunkedEncoding => _command == CHUNKED_ENCODING; |
| + |
| + int _command; |
| +} |
| + |
| +class TestServerStatus { |
|
zra
2017/04/19 17:46:13
Maybe split the state off into an enum.
bkonyi
2017/04/19 21:29:50
Done.
|
| + static const STARTED = 0; |
| + static const STOPPED = 1; |
| + static const ERROR = 2; |
| + |
| + TestServerStatus.started(this._port) : _state = STARTED; |
| + TestServerStatus.stopped() : _state = STOPPED; |
| + TestServerStatus.error() : _state = ERROR; |
| + |
| + bool get isStarted => _state == STARTED; |
| + bool get isStopped => _state == STOPPED; |
| + bool get isError => _state == ERROR; |
| + |
| + int get port => _port; |
| + |
| + int _state; |
| + int _port; |
| +} |
| + |
| +void startTestServer(SendPort replyTo) { |
| + var server = new TestServer(); |
| + server.init(); |
| + replyTo.send(server.dispatchSendPort); |
| +} |
| + |
| +class TestServer { |
| + // Echo the request content back to the response. |
| + void _echoHandler(HttpRequest request) { |
| + var response = request.response; |
| + Expect.equals("POST", request.method); |
| + response.contentLength = request.contentLength; |
| + request.listen((List<int> data) { |
| + var string = new String.fromCharCodes(data); |
| + response.write(string); |
| + response.close(); |
| + }); |
| + } |
| + |
| + // Echo the request content back to the response. |
| + void _zeroToTenHandler(HttpRequest request) { |
| + var response = request.response; |
| + String msg = "01234567890"; |
| + Expect.equals("GET", request.method); |
| + response.contentLength = msg.length; |
| + response.write(msg); |
| + response.close(); |
| + } |
| + |
| + // Return a 404. |
| + void _notFoundHandler(HttpRequest request) { |
| + var response = request.response; |
| + response.statusCode = HttpStatus.NOT_FOUND; |
| + String msg = "Page not found"; |
| + response.contentLength = msg.length; |
| + response.headers.set("Content-Type", "text/html; charset=UTF-8"); |
| + response.write(msg); |
| + response.close(); |
| + } |
| + |
| + // Return a 301 with a custom reason phrase. |
| + void _reasonForMovingHandler(HttpRequest request) { |
| + var response = request.response; |
| + response.statusCode = HttpStatus.MOVED_PERMANENTLY; |
| + response.reasonPhrase = "Don't come looking here any more"; |
| + response.close(); |
| + } |
| + |
| + // Check the "Host" header. |
| + void _hostHandler(HttpRequest request) { |
| + var response = request.response; |
| + Expect.equals(1, request.headers["Host"].length); |
| + Expect.equals("www.dartlang.org:1234", request.headers["Host"][0]); |
| + Expect.equals("www.dartlang.org", request.headers.host); |
| + Expect.equals(1234, request.headers.port); |
| + response.statusCode = HttpStatus.OK; |
| + response.close(); |
| + } |
| + |
| + void init() { |
| + // Setup request handlers. |
| + _requestHandlers = new Map(); |
| + _requestHandlers["/echo"] = _echoHandler; |
| + _requestHandlers["/0123456789"] = _zeroToTenHandler; |
| + _requestHandlers["/reasonformoving"] = _reasonForMovingHandler; |
| + _requestHandlers["/host"] = _hostHandler; |
| + _dispatchPort = new ReceivePort(); |
| + _dispatchPort.listen(dispatch); |
| + } |
| + |
| + SendPort get dispatchSendPort => _dispatchPort.sendPort; |
| + |
| + void dispatch(var message) { |
| + TestServerCommand command = message[0]; |
| + SendPort replyTo = message[1]; |
| + if (command.isStart) { |
| + try { |
| + HttpServer.bind("127.0.0.1", 0).then((server) { |
| + _server = server; |
| + _server.listen(_requestReceivedHandler); |
| + replyTo.send(new TestServerStatus.started(_server.port)); |
| + }); |
| + } catch (e) { |
| + replyTo.send(new TestServerStatus.error()); |
| + } |
| + } else if (command.isStop) { |
| + _server.close(); |
| + _dispatchPort.close(); |
| + replyTo.send(new TestServerStatus.stopped()); |
| + } else if (command.isChunkedEncoding) { |
| + _chunkedEncoding = true; |
| + } |
| + } |
| + |
| + void _requestReceivedHandler(HttpRequest request) { |
| + var requestHandler = _requestHandlers[request.uri.path]; |
| + if (requestHandler != null) { |
| + requestHandler(request); |
| + } else { |
| + _notFoundHandler(request); |
| + } |
| + } |
| + |
| + HttpServer _server; // HTTP server instance. |
| + ReceivePort _dispatchPort; |
| + Map _requestHandlers; |
| + bool _chunkedEncoding = false; |
| +} |
| + |
| +void testStartStop() { |
|
zra
2017/04/19 17:46:13
These tests are asynchronous, and you'll probably
bkonyi
2017/04/19 21:29:50
Done.
|
| + TestServerMain testServerMain = new TestServerMain(); |
| + testServerMain.setServerStartedHandler((int port) { |
| + testServerMain.close(); |
| + }); |
| + testServerMain.start(); |
| +} |
| + |
| +void testGET() { |
| + TestServerMain testServerMain = new TestServerMain(); |
| + testServerMain.setServerStartedHandler((int port) { |
| + HttpClientSync httpClient = new HttpClientSync(); |
| + var request = |
| + httpClient.getUrl(new Uri.http("127.0.0.1:$port", "/0123456789")); |
| + var response = request.close(); |
| + Expect.equals(HttpStatus.OK, response.statusCode); |
| + Expect.equals(11, response.contentLength); |
| + Expect.equals("01234567890", response.body); |
| + testServerMain.close(); |
| + }); |
| + testServerMain.start(); |
| +} |
| + |
| +void testPOST(bool chunkedEncoding) { |
| + String data = "ABCDEFGHIJKLMONPQRSTUVWXYZ"; |
| + final int kMessageCount = 10; |
| + |
| + TestServerMain testServerMain = new TestServerMain(); |
| + |
| + void runTest(int port) { |
| + int count = 0; |
| + HttpClientSync httpClient = new HttpClientSync(); |
| + void sendRequest() { |
| + var request = |
| + httpClient.postUrl(new Uri.http("127.0.0.1:$port", "/echo")); |
| + if (chunkedEncoding) { |
| + request.write(data.substring(0, 10)); |
| + request.write(data.substring(10, data.length)); |
| + } else { |
| + request.write(data); |
| + } |
| + var response = request.close(); |
| + Expect.equals(HttpStatus.OK, response.statusCode); |
| + Expect.equals(data, response.body); |
| + count++; |
| + if (count < kMessageCount) { |
| + sendRequest(); |
| + } else { |
| + testServerMain.close(); |
| + } |
| + } |
| + |
| + sendRequest(); |
| + } |
| + |
| + testServerMain.setServerStartedHandler(runTest); |
| + testServerMain.start(chunkedEncoding); |
| +} |
| + |
| +void test404() { |
| + TestServerMain testServerMain = new TestServerMain(); |
| + testServerMain.setServerStartedHandler((int port) { |
| + HttpClientSync httpClient = new HttpClientSync(); |
| + var request = |
| + httpClient.getUrl(new Uri.http("127.0.0.1:$port", "/thisisnotfound")); |
| + var response = request.close(); |
| + Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); |
| + Expect.equals("Page not found", response.body); |
| + testServerMain.close(); |
| + }); |
| + testServerMain.start(); |
| +} |
| + |
| +void testReasonPhrase() { |
| + TestServerMain testServerMain = new TestServerMain(); |
| + testServerMain.setServerStartedHandler((int port) { |
| + HttpClientSync httpClient = new HttpClientSync(); |
| + var request = |
| + httpClient.getUrl(new Uri.http("127.0.0.1:$port", "/reasonformoving")); |
| + var response = request.close(); |
| + Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); |
| + Expect.equals( |
| + "Don't come looking here any more\r\n", response.reasonPhrase); |
| + testServerMain.close(); |
| + }); |
| + testServerMain.start(); |
| +} |
| + |
| +void main() { |
| + testStartStop(); |
| + testGET(); |
| + testPOST(true); |
| + testPOST(false); |
| + test404(); |
| + testReasonPhrase(); |
| +} |