This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CannedHttp for testing, HTTP headers in response. (#13)
* Cleanup web socket API, make more idiomatic. * Export SeltzerWebSocketProvider * Bump pubspec to 0.1.0-alpha * Fix. * Add a CannedHttp implementation for testing. * Add response headers. * Fixes.
- Loading branch information
1 parent
853a5f6
commit 51c697c
Showing
9 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Changelog | ||
|
||
## 0.2.0-alpha | ||
|
||
- Added `CannedHttpResponse` and the `platform/testing.dart` library. | ||
- Added response headers to `SeltzerHttpResponse` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'dart:async'; | ||
import 'package:seltzer/src/context.dart'; | ||
import 'package:seltzer/src/interface.dart'; | ||
|
||
/// Initializes `package:seltzer/seltzer.dart` to use an in-memory fake. | ||
/// | ||
/// In test environments it's useful to have canned responses. | ||
void useSeltzerForTesting({ | ||
CannedSeltzerHttp useHttp, | ||
}) { | ||
if (useHttp != null) { | ||
setHttpPlatform(useHttp, true); | ||
} | ||
} | ||
|
||
/// A fake implementation of [SeltzerHttp]. | ||
/// | ||
/// Use [expect] to add expectations to return canned responses. | ||
class CannedSeltzerHttp extends PlatformSeltzerHttp { | ||
static String _getKey( | ||
String method, | ||
String url, | ||
Map<String, String> headers, | ||
) { | ||
return '$method|$url|$headers'; | ||
} | ||
|
||
final Map<String, _FakeHttpResponse> _expectations = | ||
<String, _FakeHttpResponse>{}; | ||
|
||
@override | ||
Future<SeltzerHttpResponse> execute( | ||
String method, | ||
String url, { | ||
Map<String, String> headers, | ||
}) { | ||
var response = _expectations.remove(_getKey(method, url, headers)); | ||
if (response == null) { | ||
throw new StateError('No expectation found for $method $url'); | ||
} | ||
return new Future<SeltzerHttpResponse>.value(response); | ||
} | ||
|
||
/// Adds an expctation for [method] [url] [requestHeaders]. | ||
void expect( | ||
String method, | ||
String url, { | ||
Map<String, String> requestHeaders: const {}, | ||
String response: '', | ||
Map<String, String> responseHeaders: const {}, | ||
}) { | ||
_expectations[_getKey(method, url, requestHeaders)] = | ||
new _FakeHttpResponse(response, responseHeaders); | ||
} | ||
} | ||
|
||
class _FakeHttpResponse implements SeltzerHttpResponse { | ||
@override | ||
final Map<String, String> headers; | ||
|
||
@override | ||
final String payload; | ||
|
||
_FakeHttpResponse(this.payload, this.headers); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: seltzer | ||
version: 0.1.0-alpha | ||
version: 0.2.0-alpha | ||
description: An elegant and rich cross-platform HTTP library for Dart. | ||
authors: | ||
- Matan Lurey <[email protected]> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'package:seltzer/platform/testing.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test('should fail if an expectation is missing', () { | ||
expect(() => new CannedSeltzerHttp().get('/404').send(), throwsStateError); | ||
}); | ||
|
||
test('should return the canned response', () async { | ||
var http = new CannedSeltzerHttp(); | ||
http.expect('GET', '/fake/url', response: 'Hello World'); | ||
expect( | ||
(await http.get('/fake/url').send().first).payload, | ||
'Hello World', | ||
); | ||
}); | ||
} |