Skip to content

Commit

Permalink
use ts.request for common test code
Browse files Browse the repository at this point in the history
  • Loading branch information
briskt committed Dec 9, 2023
1 parent 26c8bf9 commit e3d5ce4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 32 deletions.
12 changes: 2 additions & 10 deletions server/tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,10 @@ func (ts *TestSuite) Test_tenantsCreateHandler() {
for _, tt := range tests {
ts.T().Run(tt.name, func(t *testing.T) {
input := app.TenantCreateInput{Name: "new tenant"}
j, _ := json.Marshal(&input)
req := httptest.NewRequest(http.MethodPost, "/api/tenants", bytes.NewReader(j))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, "Bearer "+tt.token)

res := httptest.NewRecorder()
ts.server.ServeHTTP(res, req)
body, err := io.ReadAll(res.Body)
ts.NoError(err)
body, status := ts.request(http.MethodPost, "/api/tenants", tt.token, input)

// Assertions
ts.Equal(tt.wantStatus, res.Code, "incorrect http status, body: \n%s", body)
ts.Equal(tt.wantStatus, status, "incorrect http status, body: \n%s", body)

if tt.wantStatus != http.StatusOK {
return
Expand Down
26 changes: 4 additions & 22 deletions server/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ package server_test

import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/labstack/echo/v4"

"github.com/briskt/keygo/app"
"github.com/briskt/keygo/db"
)
Expand Down Expand Up @@ -64,17 +60,10 @@ func (ts *TestSuite) Test_GetUser() {

for _, tt := range tests {
ts.T().Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/api/users/"+tt.userID, nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, "Bearer "+tt.token)

res := httptest.NewRecorder()
ts.server.ServeHTTP(res, req)
body, err := io.ReadAll(res.Body)
ts.NoError(err)
body, status := ts.request(http.MethodGet, "/api/users/"+tt.userID, tt.token, nil)

// Assertions
ts.Equal(tt.wantStatus, res.Code, "incorrect http status, body: \n%s", body)
ts.Equal(tt.wantStatus, status, "incorrect http status, body: \n%s", body)

if tt.wantStatus != http.StatusOK {
return
Expand Down Expand Up @@ -124,17 +113,10 @@ func (ts *TestSuite) Test_GetUserList() {

for _, tt := range tests {
ts.T().Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/api/users", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, "Bearer "+tt.token)

res := httptest.NewRecorder()
ts.server.ServeHTTP(res, req)
body, err := io.ReadAll(res.Body)
ts.NoError(err)
body, status := ts.request(http.MethodGet, "/api/users", tt.token, nil)

// Assertions
ts.Equal(tt.wantStatus, res.Code, "incorrect http status, body: \n%s", body)
ts.Equal(tt.wantStatus, status, "incorrect http status, body: \n%s", body)

if tt.wantStatus != http.StatusOK {
return
Expand Down

0 comments on commit e3d5ce4

Please sign in to comment.