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

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: docs and cleanup for mock logic in tests 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 http_mock;
sethladd 2014/01/10 18:19:36 Can you pull this out and put it into its own pack
kevmoo 2014/01/10 18:51:24 Tracked for a separate CL https://code.google.com/
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 String toString() => '$runtimeType : $_headers';
50
51 // [name] must be a lower-case version of the name.
52 void _add(String name, value) {
53 if (name == HttpHeaders.IF_MODIFIED_SINCE) {
54 if (value is DateTime) {
55 ifModifiedSince = value;
56 } else if (value is String) {
57 _set(HttpHeaders.IF_MODIFIED_SINCE, value);
58 } else {
59 throw new HttpException("Unexpected type for header named $name");
60 }
61 } else {
62 _addValue(name, value);
63 }
64 }
65
66 void _addAll(String name, value) {
67 if (value is List) {
68 for (int i = 0; i < value.length; i++) {
69 _add(name, value[i]);
70 }
71 } else {
72 _add(name, value);
73 }
74 }
75
76 void _addValue(String name, Object value) {
77 List<String> values = _headers[name];
78 if (values == null) {
79 values = new List<String>();
80 _headers[name] = values;
81 }
82 if (value is DateTime) {
83 values.add(HttpDate.format(value));
84 } else {
85 values.add(value.toString());
86 }
87 }
88
89 void _set(String name, String value) {
90 assert(name == name.toLowerCase());
91 List<String> values = new List<String>();
92 _headers[name] = values;
93 values.add(value);
94 }
95
96 /*
97 * Implemented to remove editor warnings
98 */
99 dynamic noSuchMethod(Invocation invocation) {
100 print([invocation.memberName, invocation.isGetter, invocation.isSetter, invo cation.isMethod, invocation.isAccessor]);
101 return super.noSuchMethod(invocation);
102 }
103 }
104
105 class MockHttpRequest implements HttpRequest {
106 final Uri uri;
107 final MockHttpResponse response = new MockHttpResponse();
108 final HttpHeaders headers = new MockHttpHeaders();
109 final String method = 'GET';
110 final bool followRedirects;
111
112 MockHttpRequest(this.uri, {this.followRedirects: true,
113 DateTime ifModifiedSince}) {
114 if(ifModifiedSince != null) {
115 headers.ifModifiedSince = ifModifiedSince;
116 }
117 }
118
119 /*
120 * Implemented to remove editor warnings
121 */
122 dynamic noSuchMethod(Invocation invocation) =>
123 super.noSuchMethod(invocation);
124 }
125
126 class MockHttpResponse implements HttpResponse {
127 final HttpHeaders headers = new MockHttpHeaders();
128 final Completer _completer = new Completer();
129 final List<int> _buffer = new List<int>();
130 String _reasonPhrase;
131 Future _doneFuture;
132
133 MockHttpResponse() {
134 _doneFuture = _completer.future
135 .whenComplete(() {
136 assert(!_isDone);
137 _isDone = true;
138 });
139 }
140
141 bool _isDone = false;
142
143 int statusCode = HttpStatus.OK;
144
145 String get reasonPhrase => _findReasonPhrase(statusCode);
146
147 void set reasonPhrase(String value) {
148 _reasonPhrase = value;
149 }
150
151 Future get done => _doneFuture;
152
153 Future close() {
154 _completer.complete();
155 return _doneFuture;
156 }
157
158 void add(List<int> data) {
159 _buffer.addAll(data);
160 }
161
162 void addError(error, [StackTrace stackTrace]) {
163 // doesn't seem to be hit...hmm...
164 }
165
166 Future redirect(Uri location, {int status: HttpStatus.MOVED_TEMPORARILY}) {
167 this.statusCode = status;
168 headers.set(HttpHeaders.LOCATION, location.toString());
169 return close();
170 }
171
172 void write(Object obj) {
173 var str = obj.toString();
174 add(UTF8.encode(str));
175 }
176
177 /*
178 * Implemented to remove editor warnings
179 */
180 dynamic noSuchMethod(Invocation invocation) =>
181 super.noSuchMethod(invocation);
182
183 String get mockContent => UTF8.decode(_buffer);
184
185 bool get mockDone => _isDone;
186
187 // Copied from SDK http_impl.dart @ 845 on 2014-01-05
188 // TODO: file an SDK bug to expose this on HttpStatus in some way
189 String _findReasonPhrase(int statusCode) {
190 if (_reasonPhrase != null) {
191 return _reasonPhrase;
192 }
193
194 switch (statusCode) {
195 case HttpStatus.NOT_FOUND: return "Not Found";
196 default: return "Status $statusCode";
197 }
198 }
199 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698