-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from CMSgov/ig-kl-fix-default-routing
Fix default routing
- Loading branch information
Showing
5 changed files
with
94 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
// CatchAllHandler returns 404 | ||
type CatchAllHandler struct { | ||
Logger *zap.Logger | ||
} | ||
|
||
// Handle returns 404 on unexpected routes | ||
func (h CatchAllHandler) Handle() http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
h.Logger.Error(fmt.Sprintf("Unexpected route hit: %v", r.URL)) | ||
http.Error(w, "Not Found", http.StatusNotFound) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package handlers | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"net/http/httptest" | ||
) | ||
|
||
func (s HandlerTestSuite) TestCatchAllHandler() { | ||
s.Run("catch all handler always returns 404", func() { | ||
rr := httptest.NewRecorder() | ||
req, err := http.NewRequest("GET", "/notAURL", bytes.NewBufferString("")) | ||
s.NoError(err) | ||
CatchAllHandler{ | ||
Logger: s.logger, | ||
}.Handle()(rr, req) | ||
s.Equal(http.StatusNotFound, rr.Code) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package integration | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"path" | ||
) | ||
|
||
func (s *IntegrationTestSuite) TestCatchAllRoute() { | ||
client := &http.Client{} | ||
|
||
apiURL, err := url.Parse(s.server.URL) | ||
s.NoError(err, "failed to parse URL") | ||
|
||
s.Run("get /", func() { | ||
notPath, err := url.Parse(apiURL.String()) | ||
s.NoError(err, "failed to parse URL") | ||
notPath.Path = path.Join(notPath.Path, "/notapath") | ||
req, err := http.NewRequest("GET", notPath.String(), nil) | ||
s.NoError(err) | ||
|
||
resp, err := client.Do(req) | ||
|
||
s.NoError(err) | ||
s.Equal(http.StatusNotFound, resp.StatusCode) | ||
}) | ||
|
||
s.Run("get /api/v1/notapath", func() { | ||
apiURL.Path = path.Join(apiURL.Path, "/api/v1") | ||
notPath, err := url.Parse(apiURL.String()) | ||
s.NoError(err, "failed to parse URL") | ||
notPath.Path = path.Join(notPath.Path, "/notapath") | ||
req, err := http.NewRequest("GET", notPath.String(), nil) | ||
s.NoError(err) | ||
|
||
resp, err := client.Do(req) | ||
|
||
s.NoError(err) | ||
s.Equal(http.StatusNotFound, resp.StatusCode) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters