| OLD | NEW | 
| (Empty) |  | 
 |    1 // Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file | 
 |    2 // for details. All rights reserved. Use of this source code is governed by a | 
 |    3 // BSD-style license that can be found in the LICENSE file. | 
 |    4  | 
 |    5 library scheduled_server_test; | 
 |    6  | 
 |    7 import 'dart:async'; | 
 |    8 import 'dart:io'; | 
 |    9  | 
 |   10 import '../../../pkg/http/lib/http.dart' as http; | 
 |   11 import '../lib/scheduled_server.dart'; | 
 |   12 import '../lib/scheduled_test.dart'; | 
 |   13 import '../lib/src/mock_clock.dart' as mock_clock; | 
 |   14  | 
 |   15 import 'metatest.dart'; | 
 |   16 import 'utils.dart'; | 
 |   17  | 
 |   18 void main() { | 
 |   19   expectTestsPass("a server with no handlers does nothing", () { | 
 |   20     test('test', () => new ScheduledServer()); | 
 |   21   }); | 
 |   22  | 
 |   23   expectTestsPass("a server with no handlers that receives a request throws an " | 
 |   24       "error", () { | 
 |   25     var errors; | 
 |   26     test('test 1', () { | 
 |   27       currentSchedule.onException.schedule(() { | 
 |   28         errors = currentSchedule.errors; | 
 |   29       }); | 
 |   30  | 
 |   31       var server = new ScheduledServer(); | 
 |   32       expect(server.url.then((url) => http.read(url.resolve('/hello'))), | 
 |   33           completion(equals('Hello, test!'))); | 
 |   34     }); | 
 |   35  | 
 |   36     test('test 2', () { | 
 |   37       expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 
 |   38       expect(errors.length, equals(2)); | 
 |   39       expect(errors[0].error, equals("'scheduled server 0' received GET /hello " | 
 |   40           "when no more requests were expected.")); | 
 |   41       expect(errors[1].error, new isInstanceOf<HttpParserException>()); | 
 |   42     }); | 
 |   43   }, passing: ['test 2']); | 
 |   44  | 
 |   45   expectTestsPass("a handler runs when it's hit", () { | 
 |   46     test('test', () { | 
 |   47       var server = new ScheduledServer(); | 
 |   48       expect(server.url.then((url) => http.read(url.resolve('/hello'))), | 
 |   49           completion(equals('Hello, test!'))); | 
 |   50  | 
 |   51       server.handle('GET', '/hello', (request) { | 
 |   52         request.response.write('Hello, test!'); | 
 |   53         request.response.close(); | 
 |   54       }); | 
 |   55     }); | 
 |   56   }); | 
 |   57  | 
 |   58   expectTestsPass("a handler blocks the schedule on the returned future", () { | 
 |   59     test('test', () { | 
 |   60       var blockedOnFuture = false; | 
 |   61       var server = new ScheduledServer(); | 
 |   62       expect(server.url.then((url) => http.read(url.resolve('/hello'))), | 
 |   63           completion(equals('Hello, test!'))); | 
 |   64  | 
 |   65       server.handle('GET', '/hello', (request) { | 
 |   66         request.response.write('Hello, test!'); | 
 |   67         request.response.close(); | 
 |   68         return pumpEventQueue().then((_) { | 
 |   69           blockedOnFuture = true; | 
 |   70         }); | 
 |   71       }); | 
 |   72  | 
 |   73       schedule(() => expect(blockedOnFuture, isTrue)); | 
 |   74     }); | 
 |   75   }); | 
 |   76  | 
 |   77   expectTestsPass("a handler fails if it's hit too early", () { | 
 |   78     var errors; | 
 |   79     test('test 1', () { | 
 |   80       currentSchedule.onException.schedule(() { | 
 |   81         errors = currentSchedule.errors; | 
 |   82       }); | 
 |   83  | 
 |   84       var server = new ScheduledServer(); | 
 |   85       var response = server.url.then((url) => http.read(url.resolve('/hello'))); | 
 |   86       expect(response, completion(equals('Hello, test!'))); | 
 |   87  | 
 |   88       // Block the schedule until we're sure the request has hit the server. | 
 |   89       schedule(() => response); | 
 |   90  | 
 |   91       // Add a task's worth of space to avoid hitting the heuristic of waiting | 
 |   92       // for the immediately-preceding task. | 
 |   93       schedule(() => null); | 
 |   94  | 
 |   95       server.handle('GET', '/hello', (request) { | 
 |   96         request.response.write('Hello, test!'); | 
 |   97         request.response.close(); | 
 |   98       }); | 
 |   99     }); | 
 |  100  | 
 |  101     test('test 2', () { | 
 |  102       expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 
 |  103       expect(errors.length, equals(2)); | 
 |  104       expect(errors[0].error, equals("'scheduled server 0' received GET /hello " | 
 |  105           "earlier than expected.")); | 
 |  106       expect(errors[1].error, new isInstanceOf<HttpParserException>()); | 
 |  107     }); | 
 |  108   }, passing: ['test 2']); | 
 |  109  | 
 |  110   expectTestsPass("a handler waits for the immediately prior task to complete " | 
 |  111       "before checking if it's too early", () { | 
 |  112     test('test', () { | 
 |  113       var server = new ScheduledServer(); | 
 |  114       expect(server.url.then((url) => http.read(url.resolve('/hello'))), | 
 |  115           completion(equals('Hello, test!'))); | 
 |  116  | 
 |  117       // Sleeping here is unfortunate, but we want to be sure that the HTTP | 
 |  118       // request hits the server during this test without actually blocking the | 
 |  119       // task on the request completing. | 
 |  120       // | 
 |  121       // This is also a potential race condition, but hopefully a local HTTP | 
 |  122       // request won't take 1s. | 
 |  123       schedule(() => new Future.delayed(new Duration(seconds: 1))); | 
 |  124  | 
 |  125       server.handle('GET', '/hello', (request) { | 
 |  126         request.response.write('Hello, test!'); | 
 |  127         request.response.close(); | 
 |  128       }); | 
 |  129     }); | 
 |  130   }); | 
 |  131  | 
 |  132   expectTestsPass("a handler fails if the url is wrong", () { | 
 |  133     var errors; | 
 |  134     test('test 1', () { | 
 |  135       currentSchedule.onException.schedule(() { | 
 |  136         errors = currentSchedule.errors; | 
 |  137       }); | 
 |  138  | 
 |  139       var server = new ScheduledServer(); | 
 |  140       expect(server.url.then((url) => http.read(url.resolve('/hello'))), | 
 |  141           completion(equals('Hello, test!'))); | 
 |  142  | 
 |  143       server.handle('GET', '/goodbye', (request) { | 
 |  144         request.response.write('Goodbye, test!'); | 
 |  145         request.response.close(); | 
 |  146       }); | 
 |  147     }); | 
 |  148  | 
 |  149     test('test 2', () { | 
 |  150       expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 
 |  151       expect(errors.length, equals(2)); | 
 |  152       expect(errors[0].error, equals("'scheduled server 0' expected GET " | 
 |  153           "/goodbye, but got GET /hello.")); | 
 |  154       expect(errors[1].error, new isInstanceOf<HttpParserException>()); | 
 |  155     }); | 
 |  156   }, passing: ['test 2']); | 
 |  157  | 
 |  158   expectTestsPass("a handler fails if the method is wrong", () { | 
 |  159     var errors; | 
 |  160     test('test 1', () { | 
 |  161       currentSchedule.onException.schedule(() { | 
 |  162         errors = currentSchedule.errors; | 
 |  163       }); | 
 |  164  | 
 |  165       var server = new ScheduledServer(); | 
 |  166       expect(server.url.then((url) => http.head(url.resolve('/hello'))), | 
 |  167           completes); | 
 |  168  | 
 |  169       server.handle('GET', '/hello', (request) { | 
 |  170         request.response.write('Hello, test!'); | 
 |  171         request.response.close(); | 
 |  172       }); | 
 |  173     }); | 
 |  174  | 
 |  175     test('test 2', () { | 
 |  176       expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 
 |  177       expect(errors.length, equals(2)); | 
 |  178       expect(errors[0].error, equals("'scheduled server 0' expected GET " | 
 |  179           "/hello, but got HEAD /hello.")); | 
 |  180       expect(errors[1].error, new isInstanceOf<HttpParserException>()); | 
 |  181     }); | 
 |  182   }, passing: ['test 2']); | 
 |  183  | 
 |  184   expectTestsPass("a handler times out waiting to be hit", () { | 
 |  185     var clock = mock_clock.mock()..run(); | 
 |  186     var timeOfException; | 
 |  187     var errors; | 
 |  188     test('test 1', () { | 
 |  189       currentSchedule.timeout = new Duration(milliseconds: 2); | 
 |  190       currentSchedule.onException.schedule(() { | 
 |  191         timeOfException = clock.time; | 
 |  192         errors = currentSchedule.errors; | 
 |  193       }); | 
 |  194  | 
 |  195       var server = new ScheduledServer(); | 
 |  196  | 
 |  197       server.handle('GET', '/hello', (request) { | 
 |  198         request.response.write('Hello, test!'); | 
 |  199         request.response.close(); | 
 |  200       }); | 
 |  201     }); | 
 |  202  | 
 |  203     test('test 2', () { | 
 |  204       expect(clock.time, equals(2)); | 
 |  205  | 
 |  206       expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 
 |  207       expect(errors.map((e) => e.error), equals(["The schedule timed out after " | 
 |  208         "0:00:00.002000 of inactivity."])); | 
 |  209     }); | 
 |  210   }, passing: ['test 2']); | 
 |  211  | 
 |  212   expectTestsPass("multiple handlers in series respond to requests in series", | 
 |  213       () { | 
 |  214     test('test', () { | 
 |  215       var server = new ScheduledServer(); | 
 |  216       expect(server.url.then((url) { | 
 |  217         return http.read(url.resolve('/hello/1')).then((response) { | 
 |  218           expect(response, equals('Hello, request 1!')); | 
 |  219           return http.read(url.resolve('/hello/2')); | 
 |  220         }).then((response) { | 
 |  221           expect(response, equals('Hello, request 2!')); | 
 |  222           return http.read(url.resolve('/hello/3')); | 
 |  223         }).then((response) => expect(response, equals('Hello, request 3!'))); | 
 |  224       }), completes); | 
 |  225  | 
 |  226       server.handle('GET', '/hello/1', (request) { | 
 |  227         request.response.write('Hello, request 1!'); | 
 |  228         request.response.close(); | 
 |  229       }); | 
 |  230  | 
 |  231       server.handle('GET', '/hello/2', (request) { | 
 |  232         request.response.write('Hello, request 2!'); | 
 |  233         request.response.close(); | 
 |  234       }); | 
 |  235  | 
 |  236       server.handle('GET', '/hello/3', (request) { | 
 |  237         request.response.write('Hello, request 3!'); | 
 |  238         request.response.close(); | 
 |  239       }); | 
 |  240     }); | 
 |  241   }); | 
 |  242  | 
 |  243   expectTestsPass("a server that receives a request after all its handlers " | 
 |  244       "have run throws an error", () { | 
 |  245     var errors; | 
 |  246     test('test 1', () { | 
 |  247       currentSchedule.onException.schedule(() { | 
 |  248         errors = currentSchedule.errors; | 
 |  249       }); | 
 |  250  | 
 |  251       var server = new ScheduledServer(); | 
 |  252       expect(server.url.then((url) { | 
 |  253         return http.read(url.resolve('/hello/1')).then((response) { | 
 |  254           expect(response, equals('Hello, request 1!')); | 
 |  255           return http.read(url.resolve('/hello/2')); | 
 |  256         }).then((response) { | 
 |  257           expect(response, equals('Hello, request 2!')); | 
 |  258           return http.read(url.resolve('/hello/3')); | 
 |  259         }).then((response) => expect(response, equals('Hello, request 3!'))); | 
 |  260       }), completes); | 
 |  261  | 
 |  262       server.handle('GET', '/hello/1', (request) { | 
 |  263         request.response.write('Hello, request 1!'); | 
 |  264         request.response.close(); | 
 |  265       }); | 
 |  266  | 
 |  267       server.handle('GET', '/hello/2', (request) { | 
 |  268         request.response.write('Hello, request 2!'); | 
 |  269         request.response.close(); | 
 |  270       }); | 
 |  271     }); | 
 |  272  | 
 |  273     test('test 2', () { | 
 |  274       expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 
 |  275       expect(errors.length, equals(2)); | 
 |  276       expect(errors[0].error, equals("'scheduled server 0' received GET " | 
 |  277           "/hello/3 when no more requests were expected.")); | 
 |  278       expect(errors[1].error, new isInstanceOf<HttpParserException>()); | 
 |  279     }); | 
 |  280   }, passing: ['test 2']); | 
 |  281  | 
 |  282   expectTestsPass("an error in a handler doesn't cause a timeout", () { | 
 |  283     var errors; | 
 |  284     test('test 1', () { | 
 |  285       currentSchedule.onException.schedule(() { | 
 |  286         errors = currentSchedule.errors; | 
 |  287       }); | 
 |  288  | 
 |  289       var server = new ScheduledServer(); | 
 |  290       expect(server.url.then((url) => http.read(url.resolve('/hello'))), | 
 |  291           completion(equals('Hello, test!'))); | 
 |  292  | 
 |  293       server.handle('GET', '/hello', (request) { | 
 |  294         throw 'oh no'; | 
 |  295       }); | 
 |  296     }); | 
 |  297  | 
 |  298     test('test 2', () { | 
 |  299       expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 
 |  300       expect(errors.length, equals(2)); | 
 |  301       expect(errors[0].error, equals('oh no')); | 
 |  302       expect(errors[1].error, new isInstanceOf<HttpParserException>()); | 
 |  303     }); | 
 |  304   }, passing: ['test 2']); | 
 |  305 } | 
| OLD | NEW |