Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: pkg/http_server/test/http_mock.dart

Issue 124833003: pkg/http_server: return future for VirtualDirectory serveRequest (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: tweaks Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 library htt_mock;
sethladd 2014/01/09 05:27:48 is this a candidate to be its own package?
Anders Johnsen 2014/01/09 06:12:23 http_mock?
kevmoo 2014/01/10 05:02:00 YES! angular and route_hierarchial already have m
kevmoo 2014/01/10 05:02:00 Done.
2
3 import 'dart:async';
4 import 'dart:collection';
5 import 'dart:convert';
6 import 'dart:io';
7
8 class MockHttpHeaders implements HttpHeaders {
9 final Map<String, List<String>> _headers =
10 new HashMap<String, List<String>>();
11
12
13 DateTime get ifModifiedSince {
14 List<String> values = _headers[HttpHeaders.IF_MODIFIED_SINCE];
15 if (values != null) {
16 try {
17 return HttpDate.parse(values[0]);
18 } on Exception catch (e) {
19 return null;
20 }
21 }
22 return null;
23 }
24
25 void set ifModifiedSince(DateTime ifModifiedSince) {
26 // Format "ifModifiedSince" header with date in Greenwich Mean Time (GMT).
27 String formatted = HttpDate.format(ifModifiedSince.toUtc());
28 _set(HttpHeaders.IF_MODIFIED_SINCE, formatted);
29 }
30
31 ContentType contentType;
32
33 void set(String name, Object value) {
34 name = name.toLowerCase();
35 _headers.remove(name);
36 _addAll(name, value);
37 }
38
39 String value(String name) {
40 name = name.toLowerCase();
41 List<String> values = _headers[name];
42 if (values == null) return null;
43 if (values.length > 1) {
44 throw new HttpException("More than one value for header $name");
45 }
46 return values[0];
47 }
48
49 // [name] must be a lower-case version of the name.
50 void _add(String name, value) {
51 if (name == HttpHeaders.IF_MODIFIED_SINCE) {
52 if (value is DateTime) {
53 ifModifiedSince = value;
54 } else if (value is String) {
55 _set(HttpHeaders.IF_MODIFIED_SINCE, value);
56 } else {
57 throw new HttpException("Unexpected type for header named $name");
58 }
59 } else {
60 _addValue(name, value);
61 }
62 }
63
64 void _addAll(String name, value) {
65 if (value is List) {
66 for (int i = 0; i < value.length; i++) {
67 _add(name, value[i]);
68 }
69 } else {
70 _add(name, value);
71 }
72 }
73
74 void _addValue(String name, Object value) {
75 List<String> values = _headers[name];
76 if (values == null) {
77 values = new List<String>();
78 _headers[name] = values;
79 }
80 if (value is DateTime) {
81 values.add(HttpDate.format(value));
82 } else {
83 values.add(value.toString());
84 }
85 }
86
87 void _set(String name, String value) {
88 assert(name == name.toLowerCase());
89 List<String> values = new List<String>();
90 _headers[name] = values;
91 values.add(value);
92 }
93
94 /*
95 * Implemented to remove editor warnings
96 */
97 dynamic noSuchMethod(Invocation invocation) {
98 print([invocation.memberName, invocation.isGetter, invocation.isSetter, invo cation.isMethod, invocation.isAccessor]);
99 return super.noSuchMethod(invocation);
100 }
101 }
102
103 class MockHttpRequest implements HttpRequest {
104 final Uri uri;
105 final MockHttpResponse response = new MockHttpResponse();
106 final HttpHeaders headers = new MockHttpHeaders();
107 final String method = 'GET';
108
109 MockHttpRequest(this.uri, {DateTime ifModifiedSince}) {
110 if(ifModifiedSince != null) {
111 headers.ifModifiedSince = ifModifiedSince;
112 }
113 }
114
115 /*
116 * Implemented to remove editor warnings
117 */
118 dynamic noSuchMethod(Invocation invocation) =>
119 super.noSuchMethod(invocation);
120 }
121
122 class MockHttpResponse implements HttpResponse {
123 final HttpHeaders headers = new MockHttpHeaders();
124 final Completer _completer = new Completer();
125 final List<int> _buffer = new List<int>();
126 String _reasonPhrase;
127 Future _doneFuture;
128
129 MockHttpResponse() {
130 _doneFuture = _completer.future
131 .whenComplete(() {
132 assert(!_isDone);
133 _isDone = true;
134 });
135 }
136
137 bool _isDone = false;
138
139 int statusCode = HttpStatus.OK;
140
141 String get reasonPhrase => _findReasonPhrase(statusCode);
142
143 void set reasonPhrase(String value) {
144 _reasonPhrase = value;
145 }
146
147 Future get done => _doneFuture;
148
149 Future close() {
150 _completer.complete();
151 return _doneFuture;
152 }
153
154 void add(List<int> data) {
155 _buffer.addAll(data);
156 }
157
158 void addError(error, [StackTrace stackTrace]) {
159 // doesn't seem to be hit...hmm...
160 }
161
162 void write(Object obj) {
163 var str = obj.toString();
164 add(UTF8.encode(str));
165 }
166
167 /*
168 * Implemented to remove editor warnings
169 */
170 dynamic noSuchMethod(Invocation invocation) =>
171 super.noSuchMethod(invocation);
172
173 String get mockContent => UTF8.decode(_buffer);
174
175 bool get mockDone => _isDone;
176
177 // Copied from SDK http_impl.dart @ 845 on 2014-01-05
178 // TODO: file an SDK bug to expose this on HttpStatus in some way
179 String _findReasonPhrase(int statusCode) {
180 if (_reasonPhrase != null) {
181 return _reasonPhrase;
182 }
183
184 switch (statusCode) {
185 case HttpStatus.NOT_FOUND: return "Not Found";
186 default: return "Status $statusCode";
187 }
188 }
189 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698