Basically, to create a test, you just need to:
redstoneSetUp()
to load your handlersMockRequest
dispatch()
to send your requestredstoneTearDown()
to unload your handlersExample:
library services;
import 'package:redstone/redstone.dart' as app;
@app.Route("/user/:username")
helloUser(String username) => "hello, $username";
import 'package:test/test.dart';
import 'package:redstone/redstone.dart' as app;
import 'package:your_package_name/services.dart';
main() {
// Load handlers in 'services' library
setUp(() => app.redstoneSetUp([#services]));
// Remove all loaded handlers
tearDown(() => app.redstoneTearDown());
test("hello service", () {
// Create a mock request
var req = new app.MockRequest("/user/luiz");
// Dispatch the request
return app.dispatch(req).then((resp) {
// Verify the response
expect(resp.statusCode, equals(200));
expect(resp.mockContent, equals("hello, luiz"));
});
});
}
var req = new MockRequest("/service");
var req = new MockRequest("/service", queryParams: {"param": "value"});
var req = new MockRequest("/service", method: app.POST, bodyType: app.JSON, body: {
"key1": "value1",
"key2": "value2"
});
var req = new MockRequest("/service", method: app.POST, bodyType: app.FORM, body: {
"key1": "value1",
"key2": "value2"
});
import "dart:convert";
import "dart:io";
// ...
var file = new app.HttpBodyFileUpload(ContentType.parse("text/plain"),
"test.txt",
UTF8.encode("test"));
var req = new MockRequest("/service", method: app.POST, bodyType: app.FORM, body: {
"key1": "value1",
"file": file
});
var req = new MockRequest("/service",
session: new MockHttpSession("session_id", {"user": "username"}));