Message#from/to
struct Page { string title; string body; } auto expectedOutput = Page("A title", "Lorem ipsum dolor sit amet, consectetur..."); auto message = Message.from(expectedOutput); auto output = message.to!Page(); assert(output.title == expectedOutput.title, "Expected title to match"); assert(output.body == expectedOutput.body, "Expected body to match");
Message#to - bad type
struct Page { string title; string body; } struct Author { string name; } auto expectedOutput = Page("A title", "Lorem ipsum dolor sit amet, consectetur..."); auto message = Message.from(expectedOutput); TypeMismatchException gotException; try { auto output = message.to!Author(); } catch (TypeMismatchException exception) { gotException = exception; } assert(gotException.msg == "Expected type Author but got Page");
Message#createResponse - verify that response status is set
import std.conv : to; struct Ping { string message; } auto ping = Ping("Lorem ipsum..."); auto request = Message.from(ping); auto response = request.createResponse(ResponseForbidden()); assert(response.type == "ResponseForbidden", "Expected response.type to equal ResponseForbidden, got " ~ response.type); assert(response.responseStatus == ResponseStatus.FORBIDDEN, "Expected responseStatus to be ResponseStatus.FORBIDDEN got " ~ response.responseStatus.to!string); assert(response.correlationId == request.correlationId, "response.correlationId should equal request.correlationId");