httpstub

package module
v0.0.0-...-2b833e1 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2019 License: GPL-3.0 Imports: 6 Imported by: 0

README

httpstub

A wrapper package for an easier way to stub http request in golang

Examples | License

Examples

  • see godoc for more detailed examples
  srv := httpstub.NewStubServer()
	srv.StubRequest(http.MethodGet, "/healthz")
	defer srv.Close()

	url := fmt.Sprintf("%s%s", srv.URL(), "/healthz")
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		panic(err)
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	respBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Response Body: %s\n", string(respBody))
	fmt.Printf("Response Status Code: %d\n", resp.StatusCode)

	// Output:
	// Response Body: OK
	// Response Status Code: 200
}

License

GPL © [Adhitya Ramadhanus]

Documentation

Overview

Package httpstub is wrapper package of httptest Server to make it easier to stub http request to a third-party when testing a module

Example (Default)
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/AdhityaRamadhanus/httpstub"
)

func main() {
	srv := httpstub.NewStubServer()
	srv.StubRequest(http.MethodGet, "/healthz")
	defer srv.Close()

	url := fmt.Sprintf("%s%s", srv.URL(), "/healthz")
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		panic(err)
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	respBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Response Body: %s\n", string(respBody))
	fmt.Printf("Response Status Code: %d\n", resp.StatusCode)

}
Output:

Response Body: OK
Response Status Code: 200
Example (ResponseJSON)
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/AdhityaRamadhanus/httpstub"
)

func main() {
	srv := httpstub.NewStubServer()
	responseJSON := map[string]interface{}{
		"success": true,
	}
	srv.StubRequest(http.MethodGet, "/healthz", httpstub.WithResponseBodyJSON(responseJSON))
	defer srv.Close()

	url := fmt.Sprintf("%s%s", srv.URL(), "/healthz")
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		panic(err)
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	respBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Response Body: %s\n", string(respBody))
	fmt.Printf("Response Status Code: %d\n", resp.StatusCode)
}
Output:

Response Body: {"success":true}
Response Status Code: 200
Example (ResponseJSONFile)
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"

	"github.com/AdhityaRamadhanus/httpstub"
)

func main() {
	srv := httpstub.NewStubServer()
	responseJSONPath := "example_response.json"
	srv.StubRequest(http.MethodGet, "/healthz", httpstub.WithResponseBodyJSONFile(responseJSONPath))
	defer srv.Close()

	url := fmt.Sprintf("%s%s", srv.URL(), "/healthz")
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		panic(err)
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	respBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Response Body: %s\n", string(respBody))
	fmt.Printf("Response Status Code: %d\n", resp.StatusCode)
}
Output:

Response Body: {"success":true}
Response Status Code: 200

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config func(spec *spec)

Config is a helper to create spec that StubServer knows how to handle

func WithBasicAuth

func WithBasicAuth(username, password string) Config

WithBasicAuth will match only request with provided basic auth credentials

func WithBearerToken

func WithBearerToken(token string) Config

WithBearerToken will match only request with provided bearer token

func WithRequestHeaders

func WithRequestHeaders(headers map[string]string) Config

WithRequestHeaders set request headers that will be matched on an incoming matched request to StubServer

func WithResponseBody

func WithResponseBody(body []byte) Config

WithResponseBody set response body(bytes) that will be sent to client on an incoming matched request to StubServer

func WithResponseBodyFile

func WithResponseBodyFile(path string) Config

WithResponseBodyFile set response body(path to file) that will be sent to client on an incoming matched request to StubServer

func WithResponseBodyJSON

func WithResponseBodyJSON(body map[string]interface{}) Config

WithResponseBodyJSON set response body(map[string]interface{}) that will be sent to client on an incoming matched request to StubServer

func WithResponseBodyJSONFile

func WithResponseBodyJSONFile(path string) Config

WithResponseBodyJSONFile set response body(path to json) that will be sent to client on an incoming matched request to StubServer

func WithResponseBodyString

func WithResponseBodyString(body string) Config

WithResponseBodyString set response body(string) that will be sent to client on an incoming matched request to StubServer

func WithResponseCode

func WithResponseCode(statusCode int) Config

WithResponseCode set response status code that will be sent to client on an incoming matched request to StubServer

func WithResponseHeaders

func WithResponseHeaders(headers map[string]string) Config

WithResponseHeaders set response headers that will be sent to client on an incoming matched request to StubServer

type StubServer

type StubServer struct {
	// contains filtered or unexported fields
}

StubServer is a server that stub request coming and response according to a spec (see config)

func NewStubServer

func NewStubServer() *StubServer

NewStubServer Create and Run StubServer

func (*StubServer) Close

func (s *StubServer) Close()

Close the StubServer

func (*StubServer) StubRequest

func (s *StubServer) StubRequest(method, path string, options ...Config)

StubRequest takes method, path and configs to create a spec that will be matched on server

func (*StubServer) URL

func (s *StubServer) URL() string

URL return StubServer URL, your client should make a request to this URL

Jump to

Keyboard shortcuts

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