Skip to content

A simple http test server which allow the use of raw http response data for easy mocking APIs and web services. Create end-to-end tests for your http client by setting your own routes using custom patterns and a stub for each.

License

Notifications You must be signed in to change notification settings

georlav/httprawmock

Repository files navigation

godoc Tests Golang-CI

httprawmock

A simple http test server which allow the use of raw http response data for easy mocking APIs and web services. Create end-to-end tests for your http client by setting your own routes using custom patterns and a stub for each.

Examples

Please check the examples_test.go file for some basic usage examples. Also check some basic examples from another project that uses httprawmock and table driven tests.

    response := `HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Sat, 20 Nov 2021 13:39:23 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range
Access-Control-Expose-Headers: Content-Length,Content-Range

{"_id":"6198f9da97069d03e849096d","name":"Sparkle Angel","age":2,"colour":"blue"}`

    // Register your routes
	ts := httprawmock.NewServer(
		httprawmock.NewRoute(http.MethodGet, "/unicorns/{id}", []byte(response)),
	)
	defer ts.Close()

    resp, err := http.DefaultClient.Get(ts.URL + "/unicorns/6198f9da97069d03e849096d")
	if err != nil {
		return err
	}
	defer resp.Body.Close()

    b, err := io.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	fmt.Println(string(b))

Install

go get github.com/georlav/httprawmock@latest

Running tests

To run tests use

go test -race ./... -v

License

Distributed under the MIT License. See LICENSE for more information.

Contact

George Lavdanis - georlav@gmail.com

Project Link: https://github.com/georlav/httprawmock

About

A simple http test server which allow the use of raw http response data for easy mocking APIs and web services. Create end-to-end tests for your http client by setting your own routes using custom patterns and a stub for each.

Topics

Resources

License

Stars

Watchers

Forks

Languages