httprawmock

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2021 License: MIT Imports: 8 Imported by: 0

README

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.

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

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Route

type Route struct {
	Method   string
	Pattern  string
	Response []byte
}

func NewRoute

func NewRoute(method string, pattern string, response []byte) Route

NewRoute creates a new route object

method can be any valid http method, accepted values are "GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "CONNECT", "OPTIONS", "TRACE" you can also pass an empty method, this will have as result to register the given pattern under all methods

pattern you can use patterns like:

    /resource/* (match any)
	   /resource/{id} (match resource followed by and id)
	   /resource/{id:[0-9]+} (match resource followed by a numeric id)
	   /resource/{id:[a-z]+} (match resource followed by a alpha id)
	   /resource/{id:[a-z0-9]+} (match resource followed by an alphanumeric id)
	   /resource/{id}/activate
    /resource/{id}/resourceb/{uid}

You can use more complex p`atterns, package is using chi router to create routes so if you need more complex patterns you can check their docs

response must a valid raw http response

type Server

type Server struct {
	*httptest.Server
	// contains filtered or unexported fields
}

func NewServer

func NewServer(routes ...Route) *Server

NewServer starts and returns a new Server. The caller should call Close when finished, to shut it down.

Example
package main

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

	"github.com/georlav/httprawmock"
)

func main() {
	resp1, _ := os.ReadFile("testdata/post_unicorn.txt")
	resp2, _ := os.ReadFile("testdata/get_unicorns.txt")
	resp3, _ := os.ReadFile("testdata/get_unicorn.txt")
	resp4, _ := os.ReadFile("testdata/put_unicorn.txt")
	resp5, _ := os.ReadFile("testdata/delete_unicorn.txt")

	// Register your routes according to you needs
	ts := httprawmock.NewServer(
		httprawmock.NewRoute(http.MethodPost, "/unicorns/{id}", resp1),
		httprawmock.NewRoute(http.MethodGet, "/unicorns", resp2),
		httprawmock.NewRoute(http.MethodGet, "/unicorns/{id}", resp3),
		httprawmock.NewRoute(http.MethodPut, "/unicorns/{id}", resp4),
		httprawmock.NewRoute(http.MethodDelete, "/unicorns/{id}", resp5),
	)
	defer ts.Close()

	// Retrieve all registered routes (for debuging purposes)
	rts, err := ts.GetRoutes()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	for i := range rts {
		fmt.Println(rts[i])
	}

	// You can pass the server url (ts.URL) to your client implementation and send the request
	resp, err := http.DefaultClient.Get(ts.URL + "/unicorns/6198f9da97069d03e849096d")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	b, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println(string(b))

}
Output:

Method: DELETE, Pattern: /unicorns/{id}
Method: GET, Pattern: /unicorns
Method: GET, Pattern: /unicorns/{id}
Method: POST, Pattern: /unicorns/{id}
Method: PUT, Pattern: /unicorns/{id}
{"_id":"6198f9da97069d03e849096d","name":"Sparkle Angel","age":2,"colour":"blue"}

func NewUnstartedServer

func NewUnstartedServer(routes ...Route) *Server

NewUnstartedServer returns a new Server but doesn't start it.

After changing its configuration, the caller should call Start or StartTLS.

The caller should call Close when finished, to shut it down.

func (*Server) GetRoutes

func (s *Server) GetRoutes() ([]string, error)

GetRoutes returns registered routes

func (*Server) SetCustomMethodNotAllowedHandler

func (s *Server) SetCustomMethodNotAllowedHandler(h http.HandlerFunc)

SetCustomMethodNotAllowedHandler change the default method not allowed router handler

func (*Server) SetCustomNotFoundHandler

func (s *Server) SetCustomNotFoundHandler(h http.HandlerFunc)

SetCustomNotFoundHandler change the default not found router handler

func (*Server) SetNotFoundHandler

func (s *Server) SetNotFoundHandler(h http.HandlerFunc)

SetNotFoundHandler change the default not found router handler

Jump to

Keyboard shortcuts

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