Skip to content

Commit 98d166e

Browse files
gemy26pashagolub
andauthored
[-] return HTTP 405 for non-POST methods in login handler (#1226)
* Added default case for unsupported methods in login handler * fix tests --------- Co-authored-by: Pavlo Golub <pavlo.golub@gmail.com>
1 parent 6e60272 commit 98d166e

2 files changed

Lines changed: 4 additions & 6 deletions

File tree

internal/webserver/jwt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package webserver
22

33
import (
44
"errors"
5-
"fmt"
65
"net/http"
76
"time"
87

@@ -48,8 +47,9 @@ func (s *WebUIServer) handleLogin(w http.ResponseWriter, r *http.Request) {
4847
}
4948
_, err = w.Write([]byte(token))
5049

51-
case "GET":
52-
fmt.Fprintf(w, "only POST methods is allowed.")
50+
default:
51+
w.Header().Set("Allow", "POST")
52+
http.Error(w, "only POST method is allowed", http.StatusMethodNotAllowed)
5353
return
5454
}
5555
}

internal/webserver/jwt_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ func TestHandleLogin_GET(t *testing.T) {
5959
w := httptest.NewRecorder()
6060
ts.handleLogin(w, r)
6161
resp := w.Result()
62-
assert.Equal(t, http.StatusOK, resp.StatusCode)
63-
body, _ := io.ReadAll(resp.Body)
64-
assert.Equal(t, "only POST methods is allowed.", string(body))
62+
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode)
6563
}
6664

6765
func TestGenerateAndValidateJWT(t *testing.T) {

0 commit comments

Comments
 (0)