Basically, to create a test, you just need to:

  • Call redstoneSetUp() to load your handlers
  • Create a MockRequest
  • Call dispatch() to send your request
  • Inspect the returned response
  • Call redstoneTearDown() to unload your handlers

Example:

library services;

import 'package:redstone/redstone.dart' as app;

@app.Route("/user/:username")
helloUser(String username) => "hello, $username";

Source code

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"));
    });
  });
}

Source code

MockRequest Examples

  • GET request: /service
var req = new MockRequest("/service");
  • GET request: /service?param=value
var req = new MockRequest("/service", queryParams: {"param": "value"});
  • POST request: /service, JSON data
var req = new MockRequest("/service", method: app.POST, bodyType: app.JSON, body: {
  "key1": "value1",
  "key2": "value2"
});
  • POST request: /service, FORM data
var req = new MockRequest("/service", method: app.POST, bodyType: app.FORM, body: {
  "key1": "value1",
  "key2": "value2"
});
  • POST request: /service, FORM data, multipart request
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
});
  • Mocking a session
var req = new MockRequest("/service", 
  session: new MockHttpSession("session_id", {"user": "username"}));