Skip to content

Commit 0522ba5

Browse files
authored
Merge pull request #3 from jkernech/bugfix/proxy-error-handling
Enhance proxy error handling
2 parents 9fdacd3 + fdf94a5 commit 0522ba5

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func Index(c *gin.Context) {
9191
resp, err := http.Get(requestedURL)
9292

9393
if err != nil {
94-
Error(c, http.StatusNotFound, err)
94+
Error(c, http.StatusNotFound)
9595
return
9696
}
9797

@@ -100,7 +100,7 @@ func Index(c *gin.Context) {
100100
body, err := ioutil.ReadAll(resp.Body)
101101

102102
if err != nil {
103-
Error(c, http.StatusInternalServerError, err)
103+
Error(c, http.StatusNotFound, err)
104104
return
105105
}
106106

@@ -124,7 +124,7 @@ func Error(c *gin.Context, statusCode int, err ...error) {
124124
})
125125

126126
if err != nil {
127-
log.Panic(err)
127+
log.Print(err)
128128
}
129129
}
130130

main_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,35 @@ func Test404Page(t *testing.T) {
7474
}
7575
}
7676

77+
// Test that a favicon.ico request returns a 404
78+
func TestFaviconNotFound(t *testing.T) {
79+
// Create a response recorder
80+
w := httptest.NewRecorder()
81+
82+
// Get a new router
83+
r := getRouter()
84+
85+
// Define the route similar to its definition in the routes file
86+
r.GET("/favicon.ico", Index)
87+
88+
// Create a request to send to the above route
89+
req, _ := http.NewRequest("GET", "/favicon.ico", nil)
90+
91+
// Create the service and process the above request.
92+
r.ServeHTTP(w, req)
93+
94+
// Test that the http status code is 404
95+
if w.Code != http.StatusNotFound {
96+
t.Fail()
97+
}
98+
99+
// Test that the page contains `404`
100+
p, err := ioutil.ReadAll(w.Body)
101+
if err != nil || strings.Index(string(p), "404") < 0 {
102+
t.Fail()
103+
}
104+
}
105+
77106
// Test that a GET returns a 200 when an URL with a scheme is specified in the Path
78107
func TestProxyRequestExistingPage(t *testing.T) {
79108
// Create a response recorder

0 commit comments

Comments
 (0)