httpmock

package module
v0.0.0-...-bbdbf45 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2016 License: MIT Imports: 5 Imported by: 0

README

HTTPMock

A Go lib to mock HTTP responses.

Coverage Status Build Status

This is an auxiliary lib to help you simulate HTTP requests or redirect your requests - for example, if you're running tests and don't want to hit the actual destination.

Each response is represented by a DummyResponse instance, that should be defined by the tester:

type DummyResponse struct {
	Code int
	HeaderValues map[string][]string
	Body string
}

where:

Attribute Description
Code It's the Response Code; usually 200 for a successful request
HeaderValues Key-value pairs stored in the response header. Previous ContentType attribute should now be stored here, under Content-Type key
Body The body response content

If you only want a generic one-for-all response, use ServeGeneric. It will ignore the URL requested and provide the same response every time - obviously, it won't recognize an invalid address.

If you want the server it to respond to an specific URL or set of URLs, the best option is ServeMulti. The keys in the parameter map are the URLs that should be answered with the dummy response defined as its value. I think the examples below explain better.

You can also make requests to real-world sites (like Reddit, Youtube, your favorite blog etc.) and the Mock client won't redirect the request to the mock server; instead, it will work as a normal HTTP client.

Examples

You can find some usage examples on folder /examples. They are complete programs, so you can run each script to see them in action. More details about the each script can be found in /examples/README.md.

Future Features

This is my wishlist of things to include in this lib. I'm not making any promises and most of them are just random thoughts that I still need to polish before having something interesting.

  • Add form/URL processing - process values passed as POST or GET to influence the output response;
  • Work with templates - add templates to answers;

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ServeGeneric

func ServeGeneric(input DummyResponse) (*httptest.Server, *http.Client)

ServeGeneric will return the same output regardless the URL requested or anything else (body text, header and/or forms).

func ServeMulti

func ServeMulti(input map[string]DummyResponse) (*httptest.Server, *http.Client)

ServeMulti will return a response according to the path defined as key to that response in input parameter. Body and header content is ignored.

Types

type DummyResponse

type DummyResponse struct {
	Code         int
	HeaderValues map[string][]string
	Body         string
}

DummyResponse represents a response that the server can send as answer to a response.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL