Categorygithub.com/dolab/httptesting
modulepackage
0.0.1
Repository: https://github.com/dolab/httptesting.git
Documentation: pkg.go.dev

# README

httptesting

CircleCI

Golang HTTP testing client for human.

Installation

$ go get github.com/dolab/httptesting

Getting Started

package httptesting

import (
	"net/http"
	"testing"

	"github.com/dolab/httptesting"
)

// Serial mode
func Test_Client(t *testing.T) {
	host := "https://example.com"
	client := httptesting.New(host, true)

	client.Get(t, "/")
	defer client.Close()

	// verify http response status
	client.AssertOK()

	// verify http response header
	client.AssertExistHeader("Content-Length")

	// verify http response body
	client.AssertNotEmpty()
}

// Concurrency mode
func Test_Request(t *testing.T) {
	host := "https://example.com"
	client := httptesting.New(host, true)

	request := client.New(t)
	request.Get("/")

	// verify http response status
	request.AssertOK()

	// verify http response header
	request.AssertExistHeader("Content-Length")

	// verify http response body
	request.AssertNotEmpty()
}

Connected with httptest.Server

package httptesting

import (
	"net/http"
	"testing"

	"github.com/dolab/httptesting"
)

type mockServer struct {
	method	string
	path	  string
	assertion func(w http.ResponseWriter, r *http.Request)
}

func (mock *mockServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	mock.assertion(w, r)
}

func Test_Server_Client(t *testing.T) {
	method := "GET"
	uri := "/server/https"
	server := &mockServer{
		method: method,
		path: uri,
		assertion: func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("x-request-method", r.Method)
			w.WriteHeader(http.StatusOK)
			w.Write([]byte("TLS"))
		},
	}

	// return default client connected with httptest.Server
	ts := httptesting.NewServer(server, true)
	defer ts.Close()

	request := ts.New(t)
	request.Get("/server/https")

	// verify http response status
	request.AssertOK()

	// verify http response header
	request.AssertExistHeader("Content-Length")

	// verify http response body
	request.AssertContains("TLS")
}

Advantage Usage

package main

import (
	"testing"
)

func Test_Request(t *testing.T) {
	host := "https://example.com"
	client := httptesting.New(host, true)

	t.Run("GET /api/json", func(t *testing.T) {
		request := client.New(t)
		request.WithHeader("X-Mock-Client", "httptesting")

		// assume server response with following json data:
		// {"user":{"name":"httptesting","age":3},"addresses":[{"name":"china"},{"name":"USA"}]}
		request.GetJSON("/api/json", nil)

		// verify http response status
		request.AssertOK()

		// verify http response header
		request.AssertHeader("X-Mock-Client", "httptesting")

		// verify http response body with json format
		request.AssertContainsJSON("user.name", "httptesting")

		// for array
		request.AssertContainsJSON("addresses.1.name", "USA")
		request.AssertNotContainsJSON("addresses.2.name")

		// use regexp for custom matcher
		request.AssertMatch("user.*")
	})

	t.Run("POST /api/json", func(t *testing.T) {
		request := client.New(t)
		request.WithHeader("X-Mock-Client", "httptesting")

		payload := struct {
			Name string `json:"name"`
			Age  int	`json:"age"`
		}{"httptesting", 3}

		// assume server response with following json data:
		// {"data":{"name":"httptesting","age":3},"success":true}
		request.PostJSON("/api/json", payload)

		// verify http response status
		request.AssertOK()

		// verify http response header
		request.AssertHeader("X-Mock-Client", "httptesting")

		// verify http response body with json format
		request.AssertContainsJSON("data.name", "httptesting")
		request.AssertContainsJSON("data.age", 3)
		request.AssertContainsJSON("success", true)

		// use regexp for custom matcher
		request.AssertNotMatch("user.*")
	})
}

Author

Spring MC

LICENSE

The MIT License (MIT)

Copyright (c) 2016

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

# Functions

New returns an initialized *Client ready for testing.
NewRequest returns a new *Request with *Client.
NewServer returns an initialized *Client along with mocked server for testing NOTE: You MUST call client.Close() for cleanup after testing.
NewServerWithTLS returns an initialized *Client along with mocked server for testing NOTE: You MUST call client.Close() for cleanup after testing.
NewWithTLS returns an initialized *Client with custom certificate.

# Structs

Client defines request component of httptesting NOTE: Client is not safe for concurrency, please use client.New(t) after initialized.
Request defines http client for human.

# Type aliases

TransportFilter is a callback for http request injection.