DEV Community

Cover image for Testing The Endpoints 🧪
Јане Џумеркоски
Јане Џумеркоски

Posted on

Testing The Endpoints 🧪

After completing the implementation of the handlers and the endpoints, now it's time to test the all thing.

Start the project with go run main.go (main.go file is the entry point) and open http://localhost:1323/ in browser, or run the following command in your terminal

curl -X GET http://localhost:1323
Enter fullscreen mode Exit fullscreen mode

If you got the following message that means that the server is working as expected.

{
  "message":"Welcome to Go URL Shortener with Redis !🚀"
}
Enter fullscreen mode Exit fullscreen mode

Now let's create short URL for the provided long URL. For this scenario, use the POST endpoint /encode, with the following body as an example:

{
  "long_url": "https://go.dev/doc/tutorial/getting-started",
  "user_id": "UwQPr3aIf9dM5x7r"
}
Enter fullscreen mode Exit fullscreen mode
curl -X POST http://localhost:1323/encode \ 
-H 'Content-Type: application/json' \
-d '{"long_url": "https://go.dev/doc/tutorial/getting-started", "user_id": "UwQPr3aIf9dM5x7r"}'
Enter fullscreen mode Exit fullscreen mode

📝 You can use any rest client you have installed locally.

The response should look like the json below:

{
  "short_url":"http://localhost:1323/dysg5Fas"
}
Enter fullscreen mode Exit fullscreen mode

And finally, to get the initial long URL, use the GET endpoint /decode with the shortURL specified as path parameter:

curl -X GET http://localhost:1323/decode/dysg5Fas
Enter fullscreen mode Exit fullscreen mode

As response we should get JSON with the long URL corresponding to the provided short URL:

{
  "long_url":"https://go.dev/doc/tutorial/getting-started",
  "short_url":"http://localhost:1323/dysg5Fas"
}
Enter fullscreen mode Exit fullscreen mode

If everything was right, we can say that our application works flawlessly! 🎉


Originally published at projectex.dev

Top comments (4)

Collapse
 
janedzumerko profile image
Јане Џумеркоски

Hey @marcello_h
Thanks for your feedback. I agree that testing the endpoints with Go code instead of a manual check is more accurate. Do you have any suggestions on how it should be done here?

Collapse
 
marcello_h profile image
Marcelloh

If this is the URI:
go.dev/doc/tutorial/getting-started
I would use (in my own software):

result, busy := runGetRouter(t, "go.dev/doc/tutorial/getting-started")

because mall my tests for endpoint that use a GET are the same.

The basic part of that:

resp := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, path, http.NoBody)
err = router.ServeHTTPError(resp, req)
result := resp.Body.String()
assert.Nil(t, err)

Collapse
 
janedzumerko profile image
Јане Џумеркоски

Great. Thanks for taking the time to share your approach. I will update the series with one more chapter. Have a nice day!

Collapse
 
marcello_h profile image
Marcelloh

If you use Go, why don't you write tests in Go that use the endpoints and check the results. (This is actually how it should be done.) No human should watch a result to check if things work, because... humans are slow and might miss some things ;-)