class Amber::Testing::ContextBuilder

Overview

Builds HTTP::Server::Context objects for testing without a real HTTP server. Uses a builder pattern so you can chain calls to configure the request before calling #build to produce the context.

context = Amber::Testing::ContextBuilder.new
  .method("POST")
  .path("/users")
  .header("Content-Type", "application/json")
  .json_body({name: "Alice"})
  .build

Defined in:

amber/testing/context_builder.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new #

[View source]

Instance Method Detail

def body(b : String) : self #

Set the request body as a raw string.


[View source]
def build : HTTP::Server::Context #

Build and return the HTTP::Server::Context. The context is backed by an IO::Memory so the response can be read back after processing.


[View source]
def build_with_io : Tuple(HTTP::Server::Context, IO::Memory) #

Build and return a tuple of {context, io} so callers can read the raw response bytes from the IO after processing.


[View source]
def header(key : String, value : String) : self #

Add a single header to the request.


[View source]
def json_body(data) : self #

Set the request body as JSON and automatically set the Content-Type header.


[View source]
def method(m : String) : self #

Set the HTTP method (GET, POST, PUT, PATCH, DELETE, etc.)


[View source]
def params(p : Hash(String, String)) : self #

Add multiple query parameters from a hash.


[View source]
def path(p : String) : self #

Set the request path.


[View source]
def query_param(key : String, value : String) : self #

Add a query parameter that will be appended to the path.


[View source]