Skip to content

Commit eed3260

Browse files
more integration test
1 parent 7e980d3 commit eed3260

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

app/app.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ type Recipe struct {
3030
type ByPrepTime []Recipe
3131

3232
func main() {
33-
http.HandleFunc("/recipes", ReverseAggregatorProxy)
33+
http.HandleFunc("/recipes", ReverseAggregatorProxyHandler)
3434
log.Println("Server starting at port", config.Port)
3535
log.Fatal(http.ListenAndServe(":8080", nil))
3636
}
3737

3838
// Redirects the request to AllRecipeHandler / AggregatedRecipeHandler
39-
func ReverseAggregatorProxy(w http.ResponseWriter, req *http.Request) {
39+
func ReverseAggregatorProxyHandler(w http.ResponseWriter, req *http.Request) {
4040
log.Println("Incoming request", req.URL)
4141
// Specify timeout to avoid apps to hang unexpecedly since there is no timeout by default
4242
// https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779

app/app_test.go

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,55 @@
11
package main
22

33
import (
4-
// "net/http"
4+
"net/http"
5+
"net/http/httptest"
56
"reflect"
67
"strconv"
78
"testing"
89
)
910

11+
func TestReverseAggregatorProxyHandler1(t *testing.T) {
12+
req, err := http.NewRequest("GET", "/recipes", nil)
13+
if err != nil {
14+
t.Fatal(err)
15+
}
16+
rr := httptest.NewRecorder()
17+
handler := http.HandlerFunc(ReverseAggregatorProxyHandler)
18+
handler.ServeHTTP(rr, req)
19+
if status := rr.Code; status != http.StatusOK {
20+
t.Errorf("Status code: got %v want %v",
21+
status, http.StatusOK)
22+
}
23+
}
24+
25+
func TestAllRecipeHandler1(t *testing.T) {
26+
req, err := http.NewRequest("GET", "/recipes?top=10&skip=-5", nil)
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
rr := httptest.NewRecorder()
31+
handler := http.HandlerFunc(ReverseAggregatorProxyHandler)
32+
handler.ServeHTTP(rr, req)
33+
if status := rr.Code; status != http.StatusOK {
34+
t.Errorf("Status code: got %v want %v",
35+
status, http.StatusOK)
36+
}
37+
}
38+
39+
func TestAggregatedRecipeHandler2(t *testing.T) {
40+
req, err := http.NewRequest("GET", "/recipes?ids=1,2,3a,-4", nil)
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
rr := httptest.NewRecorder()
45+
handler := http.HandlerFunc(ReverseAggregatorProxyHandler)
46+
handler.ServeHTTP(rr, req)
47+
if status := rr.Code; status != http.StatusOK {
48+
t.Errorf("Status code: got %v want %v",
49+
status, http.StatusOK)
50+
}
51+
}
52+
1053
func TestFilter(t *testing.T) {
1154
type args struct {
1255
input []string

0 commit comments

Comments
 (0)