Skip to content

Commit

Permalink
Don't panic on bad lettericons/ path
Browse files Browse the repository at this point in the history
  • Loading branch information
mat committed May 7, 2017
1 parent 6319a18 commit 7b28826
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion besticon/iconserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ func alliconsHandler(w http.ResponseWriter, r *http.Request) {

func lettericonHandler(w http.ResponseWriter, r *http.Request) {
charParam, col, size := lettericon.ParseIconPath(r.URL.Path)
if charParam == "" {
if charParam == "" || col == nil || size <= 0 {
writeAPIError(w, 400, errors.New("wrong format for lettericons/ path, must look like lettericons/M-144-EFC25D.png"))
return
}

w.Header().Add(contentType, imagePNG)
Expand Down
13 changes: 13 additions & 0 deletions besticon/iconserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ func TestGetLetterIcon(t *testing.T) {
assertIntegerInInterval(t, 1500, 1800, w.Body.Len())
}

func TestGetBadLetterIconPath(t *testing.T) {
req, err := http.NewRequest("GET", "/lettericons/--120.png", nil)
if err != nil {
log.Fatal(err)
}

w := httptest.NewRecorder()
lettericonHandler(w, req)

assertStringEquals(t, "400", fmt.Sprintf("%d", w.Code))
assertStringContains(t, w.Body.String(), `wrong format for lettericons/ path`)
}

func TestGetObsoleteApiRedirect(t *testing.T) {
req, err := http.NewRequest("GET", "/api/icons?url=http%3A%2F%2Fapple.com&i_am_feeling_lucky=yes", nil)
if err != nil {
Expand Down

0 comments on commit 7b28826

Please sign in to comment.