I want to write tests for handlers in Google App Engine that use Gorilla mux to read variables from the request URL.
I understand from the documentation that you can create a fake context and request to use with testing.
I'm calling the handler directly in the test but the handler isn't seeing the path parameter as expected.
func TestRouter(t *testing.T) {
inst, _ := aetest.NewInstance(nil) //ignoring error for brevity
defer inst.Close()
//tried adding this line because the test would not work with or without it
httptest.NewServer(makeRouter())
req, _ := inst.NewRequest("GET", "/user/john#example.com/id-123", nil)
req.Header.Add("X-Requested-With", "XMLHttpRequest")
resp := httptest.NewRecorder()
restHandler(resp, req)
}
func restHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
params := mux.Vars(r)
email := params["email"]
//`email` is always empty
}
The problem is that the handler always sees an empty "email" parameter because the path is not interpreted by Gorilla mux.
The router is as below:
func makeRouter() *mux.Router {
r := mux.Router()
rest := mux.NewRouter().Headers("Authorization", "").
PathPrefix("/api").Subrouter()
app := r.Headers("X-Requested-With", "XMLHttpRequest").Subrouter()
app.HandleFunc("/user/{email}/{id}", restHandler).Methods(http.MethodGet)
//using negroni for path prefix /api
r.PathPrefx("/api").Handler(negroni.New(
negroni.HandlerFunc(authCheck), //for access control
negroni.Wrap(rest),
))
return r
}
All my searches have not gotten anything specific to App Engine unit testing with Gorilla mux.
Since what you're testing is the handler, you could just get an instance of the router and call ServeHTTP on it. Here is how it should be based on your code.
main.go
func init() {
r := makeRouter()
http.Handle("/", r)
}
func makeRouter() *mux.Router {
r := mux.NewRouter()
app := r.Headers("X-Requested-With", "XMLHttpRequest").Subrouter()
app.HandleFunc("/user/{email}/{id}", restHandler).Methods(http.MethodGet)
return r
}
func restHandler(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
email := params["email"]
fmt.Fprintf(w, email)
}
main_test.go
func TestRouter(t *testing.T) {
inst, _ := aetest.NewInstance(nil) //ignoring error for brevity
defer inst.Close()
req, _ := inst.NewRequest("GET", "/user/john#example.com/id-123", nil)
req.Header.Add("X-Requested-With", "XMLHttpRequest")
rec := httptest.NewRecorder()
r := makeRouter()
r.ServeHTTP(rec, req)
if email := rec.Body.String(); email != "john#example.com" {
t.Errorf("router failed, expected: %s, got: %s", "john#example.com", email)
}
}
Notice I removed the rest routes since that's not part of your test, but the idea would be the same. Also didn't check for errors for simplicity.
Related
I am writing the unit test case for my http APIs, i need to pass the path param to the API endpoint
GetProduct(w http.ResponseWriter, r *http.Request) {
uuidString := chi.URLParam(r, "uuid")
uuid1, err := uuid.FromString(uuidString)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(err.Error()))
return
}
}
I need to test this method and for that i need to pass a valid uuid to r http.Request, please suggest how can i do that, I tried a few options from my test class like
req.URL.Query().Set("uuid", "valid_uuid")
But it did not work. How can I test this method by passing a valid uuid to request?
Let me present my usual solution with gorilla package.
handler.go file
package httpunittest
import (
"net/http"
"github.com/gorilla/mux"
)
func GetProduct(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
uuidString, isFound := params["uuid"]
if !isFound {
w.WriteHeader(http.StatusBadRequest)
return
}
w.Write([]byte(uuidString))
}
Here, you use the function Vars to fetch all of the URL parameters present within the http.Request. Then, you've to look for the uuid key and do your business logic with it.
handler_test.go file
package httpunittest
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)
func TestGetProduct(t *testing.T) {
t.Run("WithUUID", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/products/1", nil) // note that this URL is useless
r = mux.SetURLVars(r, map[string]string{"uuid": "1"})
w := httptest.NewRecorder()
GetProduct(w, r)
assert.Equal(t, http.StatusOK, w.Result().StatusCode)
})
t.Run("Without_UUID", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/products", nil) // note that this URL is useless
w := httptest.NewRecorder()
GetProduct(w, r)
assert.Equal(t, http.StatusBadRequest, w.Result().StatusCode)
})
}
First, I used the functions provided by the httptest package of the Go Standard Library that fits well for unit testing our HTTP handlers.
Then, I used the function SetUrlVars provided by the gorilla package that allows us to set the URL parameters of an http.Request.
Thanks to this you should be able to achieve what you need!
Below is my code for main.go
func main() {
app := fiber.New()
app.Use(recover.New())
inferenceController := controllers.InferenceController
middleware := middleware.Middleware
privateRoutes := routes.PrivateRoutes{InferenceController: inferenceController,Middleware: middleware }
privateRoutes.Routes(app)
log.Fatal(app.Listen(":3000"))
}
I am trying to test this code but can't figure out the way for testing
In your test you actually need to create the app and register the relevent handlers. Then use app.Test() to call the handler. You can create body content as needed and check response codes and body content.
In this model you setup your server with just the endpoints/middleware you need for each test case. You can provide mock's around this if you need, depending on your specific use case.
For your example above, it would be something like the below, not knowing what your actual endpoints are:
func TestMyFiberEndpoiunt(t *testing.T) {
// Setup the app
app := Fiber.New()
app.Use(recover.New())
inferenceController := controllers.InferenceController
middleware := middleware.Middleware
privateRoutes := routes.PrivateRoutes{InferenceController: inferenceController,Middleware: middleware }
privateRoutes.Routes(app)
// Setup your request body
reqBody := ReqData{ SomeData: "something" }
bodyJson, _ := json.Marshal(&reqBody)
req := httptest.NewRequest("GET", "/api/v1/endpoint", bytes.NewReader(bodyJson))
resp, _ := app.Test(req, 10)
// Check the expected response code
assert.Equal(t, 200, resp.StatusCode)
// Check the body content
respBody := make(byte, resp.ContentLength)
_, _ = resp.Body.read(respBody)
assert.Equal(t, `{"data":"expected"}`, string(respBody))
}
If you need stateful data accross multiple tests for some use case, you could setup your server in a TestMain with all the needed routes and share it as a package var.
If the data marshalling seems like a lot of overhead for each test case, you can use a helper function such as:
func GetJsonTestRequestResponse(app *fiber.App, method string, url string, reqBody any) (code int, respBody map[string]any, err error) {
bodyJson := []byte("")
if reqBody != nil {
bodyJson, _ := json.Marshal(reqBody)
}
req := httptest.NewRequest(method, url, bytes.NewReader(bodyJson))
resp, err := app.Test(req, 10)
code = resp.StatusCode
// If error we're done
if err != nil {
return
}
// If no body content, we're done
if resp.ContentLength == 0 {
return
}
bodyData := make([]byte, resp.ContentLength)
_, _ = resp.Body.Read(bodyData)
err = json.Unmarshal(bodyData, &respBody)
return
}
Then tests cases look cleaner and are easier to write (imho).
type testArg struct {
Arg1 string
Arg2 int
}
func TestMyFakeEndpoint(t *testing.T) {
app := fiber.New()
defer app.Shutdown()
app.Post("/test", func(c *fiber.Ctx) error {
arg := testArg{}
_ = json.Unmarshal(c.Request().Body(), &arg)
return c.JSON(arg)
})
code, body, err := GetJsonTestRequestResponse(app, "POST", "/test", testArg{"testing", 123})
assert.Nil(t, err)
assert.Equal(t, 200, code)
assert.EqualValues(t, body["Arg1"], "testing")
assert.EqualValues(t, body["Arg2"], 123)
}
I have a simple Gin server with one of the routes called /metadata.
What the handler does is it reads a file from the system, say /etc/myapp/metadata.json and returns the JSON in the response.
But when the file is not found, handler is configured to return following error.
500: metadata.json does not exists or not readable
On my system, which has the metadata.json file, the test passes. Here is the test function I am using:
package handlers_test
import (
"net/http"
"net/http/httptest"
"testing"
"myapp/routes"
"github.com/stretchr/testify/assert"
)
func TestMetadataRoute(t *testing.T) {
router := routes.SetupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/metadata", nil)
router.ServeHTTP(w, req)
assert.NotNil(t, w.Body)
assert.Equal(t, 200, w.Code)
assert.Contains(t, w.Body.String(), "field1")
assert.Contains(t, w.Body.String(), "field2")
assert.Contains(t, w.Body.String(), "field3")
assert.Contains(t, w.Body.String(), "field4")
}
But on CI environment, the test would fail because it won't find metadata.json. And would return the configured error.
What can be done?
I have this handler:
func GetMetadata(c *gin.Context) {
// read the info
content, err := ioutil.ReadFile("/etc/myapp/metadata.json")
if err != nil {
c.JSON(http.StatusInternalServerError,
gin.H{"error": "metadata.json does not exists or not readable"})
return
}
// deserialize to json
var metadata models.Metadata
err = json.Unmarshal(content, &metadata)
if err != nil {
c.JSON(http.StatusInternalServerError,
gin.H{"error": "unable to parse metadata.json"})
return
}
c.JSON(http.StatusOK, metadata)
}
What Volker is suggesting is to use a package-level unexported variable. You give it a fixed default value, corresponding to the path you need in production, and then simply overwrite that variable in your unit test.
handler code:
var metadataFilePath = "/etc/myapp/metadata.json"
func GetMetadata(c *gin.Context) {
// read the info
content, err := ioutil.ReadFile(metadataFilePath)
// ... rest of code
}
test code:
func TestMetadataRoute(t *testing.T) {
metadataFilePath = "testdata/metadata_test.json"
// ... rest of code
}
This is a super-simple solution. There are ways to improve on this, but all are variations of how to inject any variable in a Gin handler. For simple request-scoped configuration, what I usually do is to inject the variable into the Gin context. This requires slightly refactoring some of your code:
router setup code with middleware for production
func SetupRouter() {
r := gin.New()
r.GET("/metadata", MetadataPathMiddleware("/etc/myapp/metadata.json"), GetMetadata)
// ... rest of code
}
func MetadataPathMiddleware(path string) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("_mdpath", path)
}
}
handler code extracting the path from context:
func GetMetadata(c *gin.Context) {
metadataFilePath := c.GetString("_mdpath")
content, err := ioutil.ReadFile(metadataFilePath)
// ... rest of code
}
test code which you should refactor to test the handler only (more details: How to unit test a Go Gin handler function?):
func TestMetadataRoute(t *testing.T) {
// create Gin test context
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
// inject test value into context
c.Set("_mdpath", "testdata/metadata_test.json")
// just test handler, the passed context holds the test value
GetMetadata(c)
// ... assert
}
Note: setting context values with string keys is somewhat discouraged, however the Gin context accepts only string keys.
I just getting started learning Golang and PostgreSQL. For now, I tried to make Unit testing for CreateTodo function.
My CreateTodo function is
func CreateTodo(w http.ResponseWriter, r *http.Request) {
CreateTodo := &models.Todo{}
utils.ParseBody(r, CreateTodo)
CreateTodoList := CreateTodo.CreateTodo()
res, _ := json.Marshal(CreateTodoList)
w.WriteHeader(http.StatusOK)
w.Write(res)
}
I tried to make Unit Test for this function... So far I wrote some codes like
func TestCreateTodo(t *testing.T) {
dbData := &models.Todo{
Title: "test-title-console-check",
Description: "test-description-console-check",
Condition: true,
}
utils.ParseBody(r, dbData) // r should be r *http.Request
submittedTodo := dbData.CreateTodo()
res, _ := json.Marshal(submittedTodo)
r.WriteHeader(http.StatusOK) // r should be r *http.Request
r.Write(res)
fmt.Println("res: ", res)
}
This is ParseBodu function in utils folder
func ParseBody(r *http.Request, x interface{}) {
if body, err := ioutil.ReadAll(r.Body); err == nil {
if err := json.Unmarshal([]byte(body), x); err != nil {
return
}
}
}
Here, I have a problem with passing net/http(r *http.Request). I am not sure how to pass this function like argument... I tried to receive it in TestCreateTodo(t *testing.T, r *http.Request) but not working what I expected.
Is there any way to unit test for CreateTodo function??
I really appreciate your help!
Edit 1]
I tried to make a global variable
var readData *http.Request
var writeData http.ResponseWriter
and using it in the function. The reason why I make it global variables is that I usually use it in the funcs like <w http.ResponseWriter, r *http.Request>, so I thought I can use as global vars too.
so I edit my code as
var readData *http.Request
var writeData http.ResponseWriter
func TestCreateTodo(t *testing.T) {
// w := httptest.NewRecorder()
dbData := &models.Todo{
Title: "test-title-console-check",
Description: "test-description-console-check",
Condition: true,
}
utils.ParseBody(readData, dbData)
submittedTodo := dbData.CreateTodo()
res, _ := json.Marshal(submittedTodo)
writeData.WriteHeader(http.StatusOK)
writeData.Write(res)
fmt.Println("res: ", res)
}
But I got this error
As mentioned by Volker, you need to create an http request. So you are missing this line:
req, err := http.NewRequest("GET", <your endpoint>, <your body>)
As shown by the Go http package documentation, the body must be passed as a stream of bytes. You can use bytes.Buffer for this:
var body bytes.Buffer
err := json.NewEncoder(&body).Encode(dbData)
After making your request, you need to initiate a response recorder and define the handler:
res := httptest.NewRecorder()
handler := http.HandlerFunc(<your handler>)
handler.ServeHTTP(res, req)
Then you can check if your response was as expected with the assert package.
~ Zoe ~
I am trying to add form variables to a Go http request.
Here's how my Go test looks:
func sample_test(t *testing.T) {
handler := &my_listener_class{}
reader := strings.NewReader("number=2")
req, _ := http.NewRequest("POST", "/my_url", reader)
w := httptest.NewRecorder()
handler.function_to_test(w, req)
if w.Code != http.StatusOK {
t.Errorf("Home page didn't return %v", http.StatusOK)
}
}
The issue is that the form data never gets passed on to the function I need to test.
The other relevant function is:
func (listener *my_listener_class) function_to_test(writer http.ResponseWriter, request *http.Request) {
...
}
I am using Go version go1.3.3 darwin/amd64.
You need to add a Content-Type header to the request so the handler will know how to treat the POST body data:
req, _ := http.NewRequest("POST", "/my_url", reader) //BTW check for error
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")