I'm trying to write tests for a microservice I wrote using httprouter. I've previously tried to use the answers given here: How to write test with httprouter but that doesn't seem too relevant since I don't use controllers in my microservice. Any help would be greatly appreciated.
Here's what I have so far:
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/julienschmidt/httprouter"
)
func TestCreateURL(t *testing.T) {
expected := `{"original_url":"https://www.google.com", "short_url":"https://morning-retreat-24523.herokuapp.com/get/3578"}`
handler := createURL
router := httprouter.New()
router.GET("/new/*url", handler)
req, err := http.NewRequest("GET", "/new/https://www.google.com", nil)
if err != nil {
fmt.Println(err)
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
if rr.Body.String() != expected {
t.Errorf("Handler returned unexpected body: Got %v but want %v",
rr.Body.String(), expected)
}
}
createURL is a function like this (but with a lot more in it obviously):
func createURL(res http.ResponseWriter, req *http.Request, ps httprouter.Params) {
}