-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathhelpers_test.go
More file actions
55 lines (44 loc) · 1.08 KB
/
helpers_test.go
File metadata and controls
55 lines (44 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package helpers
import (
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/gorilla/mux"
)
// SetTestDelay sets a delay for testing slow network conditions
func SetTestDelay(delay time.Duration) func() {
originalDelay := os.Getenv("DEBUG_DELAY")
err := os.Setenv("DEBUG_DELAY", delay.String())
if err != nil {
panic("Failed to set DEBUG_DELAY environment variable: " + err.Error())
}
return func() {
if originalDelay == "" {
err = os.Unsetenv("DEBUG_DELAY")
} else {
err = os.Setenv("DEBUG_DELAY", originalDelay)
}
if err != nil {
panic("Failed to unset DEBUG_DELAY environment variable: " + err.Error())
}
}
}
func TestGetIntParam(t *testing.T) {
req, _ := http.NewRequest("GET", "/test/123", nil)
rr := httptest.NewRecorder()
r := mux.NewRouter()
r.HandleFunc("/test/{test_id}", mockParam)
r.ServeHTTP(rr, req)
if rr.Code != 200 {
t.Errorf("Response code should be 200 %d", rr.Code)
}
}
func mockParam(w http.ResponseWriter, r *http.Request) {
_, err := GetIntParam("test_id", w, r)
if err != nil {
return
}
w.WriteHeader(200)
}