Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Route ¶
func NewRoute ¶
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 ¶
func NewServer ¶
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 ¶
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) 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