Skip to content

Commit

Permalink
fix body size
Browse files Browse the repository at this point in the history
  • Loading branch information
justlorain committed Dec 26, 2024
1 parent dcb3197 commit 2bb4299
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions auth_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,20 +330,20 @@ func TestLoginHandler(t *testing.T) {
handler := hertzHandler(authMiddleware)

body := bytes.NewReader([]byte("{\"username\": \"admin\"}"))
w := ut.PerformRequest(handler, http.MethodPost, "/login", &ut.Body{Body: body, Len: -1}, ut.Header{Key: "Content-Type", Value: "application/json"})
w := ut.PerformRequest(handler, http.MethodPost, "/login", &ut.Body{Body: body, Len: body.Len()}, ut.Header{Key: "Content-Type", Value: "application/json"})
resp := w.Result()
assert.DeepEqual(t, ErrMissingLoginValues.Error(), gjson.Get(string(resp.BodyBytes()), "message").String())
assert.DeepEqual(t, http.StatusUnauthorized, w.Code)
assert.DeepEqual(t, "application/json; charset=utf-8", string(resp.Header.ContentType()))

body = bytes.NewReader([]byte("{\"username\": \"admin\",\"password\": \"test\"}"))
w = ut.PerformRequest(handler, http.MethodPost, "/login", &ut.Body{Body: body, Len: -1}, ut.Header{Key: "Content-Type", Value: "application/json"})
w = ut.PerformRequest(handler, http.MethodPost, "/login", &ut.Body{Body: body, Len: body.Len()}, ut.Header{Key: "Content-Type", Value: "application/json"})
resp = w.Result()
assert.DeepEqual(t, ErrFailedAuthentication.Error(), gjson.Get(string(resp.BodyBytes()), "message").String())
assert.DeepEqual(t, http.StatusUnauthorized, w.Code)

body = bytes.NewReader([]byte("{\"username\": \"admin\",\"password\": \"admin\"}"))
w = ut.PerformRequest(handler, http.MethodPost, "/login", &ut.Body{Body: body, Len: -1}, ut.Header{Key: "Content-Type", Value: "application/json"})
w = ut.PerformRequest(handler, http.MethodPost, "/login", &ut.Body{Body: body, Len: body.Len()}, ut.Header{Key: "Content-Type", Value: "application/json"})
resp = w.Result()
assert.DeepEqual(t, "login successfully", gjson.Get(string(resp.BodyBytes()), "message").String())
assert.DeepEqual(t, http.StatusOK, w.Code)
Expand Down Expand Up @@ -693,15 +693,15 @@ func TestClaimsDuringAuthorization(t *testing.T) {
assert.DeepEqual(t, http.StatusOK, w.Code)

body := bytes.NewReader([]byte("{\"username\": \"admin\",\"password\": \"admin\"}"))
w = ut.PerformRequest(handler, http.MethodPost, "/login", &ut.Body{Body: body, Len: -1}, ut.Header{Key: "Content-Type", Value: "application/json"})
w = ut.PerformRequest(handler, http.MethodPost, "/login", &ut.Body{Body: body, Len: body.Len()}, ut.Header{Key: "Content-Type", Value: "application/json"})
resp := w.Result()
assert.DeepEqual(t, http.StatusOK, w.Code)

w = ut.PerformRequest(handler, http.MethodGet, "/auth/hello", nil, ut.Header{Key: "Authorization", Value: "Bearer " + gjson.Get(string(resp.BodyBytes()), "token").String()})
assert.DeepEqual(t, http.StatusOK, w.Code)

body = bytes.NewReader([]byte("{\"username\": \"test\",\"password\": \"test\"}"))
w = ut.PerformRequest(handler, http.MethodPost, "/login", &ut.Body{Body: body, Len: -1}, ut.Header{Key: "Content-Type", Value: "application/json"})
w = ut.PerformRequest(handler, http.MethodPost, "/login", &ut.Body{Body: body, Len: body.Len()}, ut.Header{Key: "Content-Type", Value: "application/json"})
resp = w.Result()
assert.DeepEqual(t, http.StatusOK, w.Code)

Expand Down

0 comments on commit 2bb4299

Please sign in to comment.