2020-12-03 14:51:15 +01:00
|
|
|
package helpers
|
2016-01-05 00:32:53 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2017-03-14 16:50:14 +01:00
|
|
|
|
2017-04-18 18:06:58 +02:00
|
|
|
"github.com/gorilla/mux"
|
2016-01-05 00:32:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetIntParam(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/test/123", nil)
|
2017-04-18 18:06:58 +02:00
|
|
|
rr := httptest.NewRecorder()
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2017-04-18 18:06:58 +02:00
|
|
|
r := mux.NewRouter()
|
|
|
|
r.HandleFunc("/test/{test_id}", mockParam)
|
|
|
|
r.ServeHTTP(rr, req)
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2017-04-18 18:06:58 +02:00
|
|
|
if rr.Code != 200 {
|
|
|
|
t.Errorf("Response code should be 200 %d", rr.Code)
|
2016-01-05 00:32:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 18:06:58 +02:00
|
|
|
func mockParam(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := GetIntParam("test_id", w, r)
|
2016-01-05 00:32:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-18 18:06:58 +02:00
|
|
|
w.WriteHeader(200)
|
2016-01-05 00:32:53 +01:00
|
|
|
}
|