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.
Finish implementation and simple test (#21)
- Loading branch information
1 parent
ef2a542
commit eada46e
Showing
11 changed files
with
289 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
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 +1,13 @@ | ||
import 'package:seltzer/src/context.dart'; | ||
import 'package:seltzer/src/interface/http.dart'; | ||
|
||
export 'package:seltzer/src/testing/record.dart' show SeltzerHttpRecorder; | ||
export 'package:seltzer/src/testing/replay.dart' show ReplaySeltzerHttp; | ||
|
||
/// Initializes `package:seltzer/seltzer.dart` to use [implementation]. | ||
/// | ||
/// This is appropriate for test implementations that want to use an existing | ||
/// implementation, such as a [ReplaySeltzerHttp]. | ||
void useSeltzerForTesting(SeltzerHttp implementation) { | ||
setHttpPlatform(implementation); | ||
} |
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
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,46 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:reply/reply.dart'; | ||
import 'package:seltzer/seltzer.dart'; | ||
import 'package:seltzer/src/interface/http_request.dart'; | ||
|
||
/// An interceptor/delegate [SeltzerHttp] that records request/response pairs. | ||
class SeltzerHttpRecorder extends SeltzerHttpHandler { | ||
final SeltzerHttpHandler _delegate; | ||
final Recorder<SeltzerHttpRequest, SeltzerHttpResponse> _recorder; | ||
|
||
factory SeltzerHttpRecorder(SeltzerHttpHandler delegate) { | ||
return new SeltzerHttpRecorder._( | ||
delegate, | ||
new Recorder<SeltzerHttpRequest, SeltzerHttpResponse>( | ||
requestEquality: const SeltzerHttpRequestEquality(), | ||
), | ||
); | ||
} | ||
|
||
SeltzerHttpRecorder._(this._delegate, this._recorder); | ||
|
||
@override | ||
Stream<SeltzerHttpResponse> handle( | ||
SeltzerHttpRequest request, [ | ||
Object payload, | ||
]) { | ||
SeltzerHttpResponse last; | ||
final transformer = new StreamTransformer.fromHandlers( | ||
handleData: (event, sink) { | ||
last = event; | ||
sink.add(event); | ||
}, | ||
handleDone: (sink) { | ||
_recorder.given(request).reply(last).once(); | ||
sink.close(); | ||
}, | ||
); | ||
return _delegate.handle(request, payload).transform(transformer); | ||
} | ||
|
||
/// Returns all recorded request/response pairs. | ||
Recording<SeltzerHttpRequest, SeltzerHttpResponse> toRecording() { | ||
return _recorder.toRecording(); | ||
} | ||
} |
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,37 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:reply/reply.dart'; | ||
import 'package:seltzer/seltzer.dart'; | ||
import 'package:seltzer/src/interface/http_request.dart'; | ||
|
||
/// An implementation of [SeltzerHttp] that plays back a recording. | ||
class ReplaySeltzerHttp extends SeltzerHttp { | ||
final Recording<SeltzerHttpRequest, SeltzerHttpResponse> _recording; | ||
|
||
/// Create a new [ReplaySeltzerHttp] from a previous [recording]. | ||
factory ReplaySeltzerHttp( | ||
Recording<SeltzerHttpRequest, SeltzerHttpResponse> recording, | ||
) = ReplaySeltzerHttp._; | ||
|
||
/// Creates a new [ReplySeltzerHttp] from [pairs] of request/responses. | ||
factory ReplaySeltzerHttp.fromMap( | ||
Map<SeltzerHttpRequest, SeltzerHttpResponse> pairs, | ||
) { | ||
final recorder = new Recorder<SeltzerHttpRequest, SeltzerHttpResponse>( | ||
requestEquality: const SeltzerHttpRequestEquality(), | ||
); | ||
pairs.forEach((request, response) { | ||
recorder.given(request).reply(response).once(); | ||
}); | ||
return new ReplaySeltzerHttp._(recorder.toRecording()); | ||
} | ||
|
||
ReplaySeltzerHttp._(this._recording); | ||
|
||
@override | ||
Stream<SeltzerHttpResponse> handle(SeltzerHttpRequest request, [_]) { | ||
return new Stream<SeltzerHttpResponse>.fromIterable([ | ||
_recording.reply(request), | ||
]); | ||
} | ||
} |
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,15 +1,19 @@ | ||
name: seltzer | ||
version: 0.2.3-alpha | ||
version: 0.2.4-alpha | ||
description: An elegant and rich cross-platform HTTP library for Dart. | ||
authors: | ||
- Matan Lurey <[email protected]> | ||
- Kendal Harland <[email protected]> | ||
homepage: https://github.com/matanlurey/seltzer | ||
|
||
environment: | ||
sdk: ">=1.8.0 <2.0.0" | ||
|
||
dependencies: | ||
meta: "^1.0.4" | ||
reply: "0.1.2-dev" | ||
quiver: "^0.23.0" | ||
|
||
dev_dependencies: | ||
dart_style: ">=0.2.10" | ||
test: ">=0.12.15" |
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,45 @@ | ||
@TestOn('vm') | ||
import 'dart:convert'; | ||
|
||
import 'package:seltzer/platform/testing.dart'; | ||
import 'package:seltzer/platform/vm.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
const _echoUrl = 'http://localhost:9090'; | ||
|
||
main() { | ||
SeltzerHttp http; | ||
SeltzerHttpRecorder recorder; | ||
|
||
setUp(() { | ||
recorder = new SeltzerHttpRecorder(const VmSeltzerHttp()); | ||
http = recorder.asHttpClient(); | ||
}); | ||
|
||
runPingTest() async { | ||
final response = await http.post('$_echoUrl/ping').send().last; | ||
expect(JSON.decode(response.readAsString()), { | ||
'data': '', | ||
'headers': {}, | ||
'method': 'POST', | ||
'url': '/ping', | ||
}); | ||
} | ||
|
||
test('should record a request/response', () async { | ||
await runPingTest(); | ||
final recording = recorder.toRecording(); | ||
|
||
expect( | ||
recording.hasRecord(new SeltzerHttpRequest( | ||
'POST', | ||
'$_echoUrl/ping', | ||
)), | ||
isTrue, | ||
); | ||
|
||
// Now lets try replaying without a real HTTP connection. | ||
http = new ReplaySeltzerHttp(recording); | ||
await runPingTest(); | ||
}); | ||
} |
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,85 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:seltzer/seltzer.dart'; | ||
import 'package:seltzer/platform/testing.dart'; | ||
|
||
import '../common/http.dart'; | ||
|
||
main() { | ||
useSeltzerForTesting( | ||
new ReplaySeltzerHttp.fromMap({ | ||
// "should make a valid DELETE request" | ||
new SeltzerHttpRequest( | ||
'DELETE', | ||
'http://localhost:9090/das/fridge/lacroix', | ||
): new SeltzerHttpResponse.fromString(JSON.encode({ | ||
'headers': {}, | ||
'method': 'DELETE', | ||
'url': '/das/fridge/lacroix', | ||
'data': '', | ||
})), | ||
|
||
// "should make a valid GET request" | ||
new SeltzerHttpRequest( | ||
'GET', | ||
'http://localhost:9090/flags.json', | ||
): new SeltzerHttpResponse.fromString(JSON.encode({ | ||
'headers': {}, | ||
'method': 'GET', | ||
'url': '/flags.json', | ||
'data': '', | ||
})), | ||
|
||
// "should make a valid PATCH request" | ||
new SeltzerHttpRequest( | ||
'PATCH', | ||
'http://localhost:9090/pants/up', | ||
): new SeltzerHttpResponse.fromString(JSON.encode({ | ||
'headers': {}, | ||
'method': 'PATCH', | ||
'url': '/pants/up', | ||
'data': '', | ||
})), | ||
|
||
// "should make a valid POST request" | ||
new SeltzerHttpRequest( | ||
'POST', | ||
'http://localhost:9090/users/clear', | ||
): new SeltzerHttpResponse.fromString(JSON.encode({ | ||
'headers': {}, | ||
'method': 'POST', | ||
'url': '/users/clear', | ||
'data': '', | ||
})), | ||
|
||
// "should make a valid PUT request" | ||
new SeltzerHttpRequest( | ||
'PUT', | ||
'http://localhost:9090/pants/on', | ||
): new SeltzerHttpResponse.fromString(JSON.encode({ | ||
'headers': {}, | ||
'method': 'PUT', | ||
'url': '/pants/on', | ||
'data': '', | ||
})), | ||
|
||
// "should send an HTTP header" | ||
new SeltzerHttpRequest( | ||
'GET', | ||
'http://localhost:9090', | ||
headers: { | ||
'Authorization': 'abc123', | ||
}, | ||
): new SeltzerHttpResponse.fromString(JSON.encode({ | ||
'headers': { | ||
'Authorization': 'abc123', | ||
}, | ||
'method': 'GET', | ||
'url': '/', | ||
'data': '', | ||
})) | ||
}), | ||
); | ||
|
||
runHttpTests(); | ||
} |