Documentation ¶
Overview ¶
Provides helper to test http handlers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( //Handler that will be executed for test cases. Service http.HandlerFunc //Test context T *testing.T )
Functions ¶
func Error ¶
Test each assert and error on each failed. Using only plain == to compare expected and actual values. Error message has format: "Expected [%expected] %description but got [%actual]".
Example ¶
package main import ( "errors" "github.com/dooman87/kolibri/test" "testing" ) func main() { var t = new(testing.T) test.Error(t, //Error message will be: Expected [1] apples but got [6] test.Equal(1, 6, "apples"), //Error message will be: Expected price to be not equal to 2.99 test.NotEqual(2.99, 2.99, "price"), //Error message will be: Expected error to be nil test.Nil(errors.New("ERROR"), "error"), //Error message will be: Expected student name to not be nil test.NotNil(nil, "student name"), ) }
Output:
func NewRequest ¶
Wraps http.NewRequest to process error case. If http.NewRequest returns error then execution interrupts with T.Fatal()
func RunRequests ¶
func RunRequests(testCases []TestCase)
Runs all test cases. Will fail if test.T is nil.
Example ¶
package main import ( "github.com/dooman87/kolibri/test" "net/http" "net/http/httptest" "testing" ) func main() { //This is service that we want to test. It should be a type of http.HandlerFunc. test.Service = func(resp http.ResponseWriter, req *http.Request) { resp.Write([]byte("OK")) } test.T = &testing.T{} //Creating set of tests that we want to run. //Each test is a struct that contains endpoint, expected response status, description, optional handler. testCases := []test.TestCase{ { Url: "http://localhost:8080", Description: "Should return 200", }, { Url: "http://localhost:8080", Description: "Should return OK in body", Handler: func(w *httptest.ResponseRecorder, t *testing.T) { if w.Body.String() != "OK" { t.Errorf("Expected %s but got %s", "OK", w.Body.String()) } }, }, { Request: test.NewRequest("GET", "http://localhost:8080", nil), Description: "Should return 200", }, } //Running all test cases. test.RunRequests(testCases) }
Output:
Types ¶
type Assert ¶
type ResponseHandler ¶
type ResponseHandler func(w *httptest.ResponseRecorder, t *testing.T)
type TestCase ¶
type TestCase struct { //You can use this field in case you need simple GET request //or pass request using Request field. Url string //Request to execute. If nil then Url will be used and GET request will be executed. Request *http.Request //Expected respose code. If not passed then will check for 200. ExpectedCode int Description string //Optional handler that will be called at the end of the test case. Handler ResponseHandler }
Describes test case. Each test case will be executed as http request against Service handler.
Click to show internal directories.
Click to hide internal directories.