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 ¶
- type Config
- func WithBasicAuth(username, password string) Config
- func WithBearerToken(token string) Config
- func WithRequestHeaders(headers map[string]string) Config
- func WithResponseBody(body []byte) Config
- func WithResponseBodyFile(path string) Config
- func WithResponseBodyJSON(body map[string]interface{}) Config
- func WithResponseBodyJSONFile(path string) Config
- func WithResponseBodyString(body string) Config
- func WithResponseCode(statusCode int) Config
- func WithResponseHeaders(headers map[string]string) Config
- type StubServer
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 ¶
WithBasicAuth will match only request with provided basic auth credentials
func WithBearerToken ¶
WithBearerToken will match only request with provided bearer token
func WithRequestHeaders ¶
WithRequestHeaders set request headers that will be matched on an incoming matched request to StubServer
func WithResponseBody ¶
WithResponseBody set response body(bytes) that will be sent to client on an incoming matched request to StubServer
func WithResponseBodyFile ¶
WithResponseBodyFile set response body(path to file) that will be sent to client on an incoming matched request to StubServer
func WithResponseBodyJSON ¶
WithResponseBodyJSON set response body(map[string]interface{}) that will be sent to client on an incoming matched request to StubServer
func WithResponseBodyJSONFile ¶
WithResponseBodyJSONFile set response body(path to json) that will be sent to client on an incoming matched request to StubServer
func WithResponseBodyString ¶
WithResponseBodyString set response body(string) that will be sent to client on an incoming matched request to StubServer
func WithResponseCode ¶
WithResponseCode set response status code that will be sent to client on an incoming matched request to StubServer
func WithResponseHeaders ¶
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 (*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