restful-routefunction_test.go 638 B

1234567891011121314151617181920212223242526272829
  1. package main
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "github.com/emicklei/go-restful"
  7. )
  8. // This example show how to test one particular RouteFunction (getIt)
  9. // It uses the httptest.ResponseRecorder to capture output
  10. func getIt(req *restful.Request, resp *restful.Response) {
  11. resp.WriteHeader(204)
  12. }
  13. func TestCallFunction(t *testing.T) {
  14. httpReq, _ := http.NewRequest("GET", "/", nil)
  15. req := restful.NewRequest(httpReq)
  16. recorder := new(httptest.ResponseRecorder)
  17. resp := restful.NewResponse(recorder)
  18. getIt(req, resp)
  19. if recorder.Code != 204 {
  20. t.Fatalf("Missing or wrong status code:%d", recorder.Code)
  21. }
  22. }