-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Exclude some files from coverage report * Exclude auth callback handler (it's already covered on other layer) * Add a bit of tests and git rid of unused code * Use `make test` in test job * Fix temp dir for coverage profile
- Loading branch information
Showing
6 changed files
with
108 additions
and
21 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
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 |
---|---|---|
|
@@ -33,3 +33,6 @@ go.work | |
/.dbschema/ | ||
.env | ||
/bin/ | ||
|
||
/tests/*.cov | ||
/tests/*.cov_tmp |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package server_test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/defer-panic/url-shortener-api/internal/model" | ||
"github.com/defer-panic/url-shortener-api/internal/server" | ||
"github.com/defer-panic/url-shortener-api/internal/shorten" | ||
"github.com/defer-panic/url-shortener-api/internal/storage/shortening" | ||
"github.com/labstack/echo/v4" | ||
. "github.com/samber/mo" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHandleStats(t *testing.T) { | ||
t.Run("returns shortening with given identifier", func(t *testing.T) { | ||
var ( | ||
provider = shorten.NewService(shortening.NewInMemory()) | ||
handler = server.HandleStats(provider) | ||
recorder = httptest.NewRecorder() | ||
request = httptest.NewRequest("GET", "/stats/abc", nil) | ||
e = echo.New() | ||
c = e.NewContext(request, recorder) | ||
) | ||
|
||
addUserToCtx(c) | ||
|
||
c.SetPath("/stats/:identifier") | ||
c.SetParamNames("identifier") | ||
c.SetParamValues("abc") | ||
|
||
_, err := provider.Shorten( | ||
context.Background(), | ||
model.ShortenInput{ | ||
Identifier: Some("abc"), | ||
RawURL: "https://google.com", | ||
CreatedBy: "user", | ||
}, | ||
) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, handler(c)) | ||
|
||
var s model.Shortening | ||
require.NoError(t, json.NewDecoder(recorder.Body).Decode(&s)) | ||
|
||
assert.Equal(t, "abc", s.Identifier) | ||
assert.Equal(t, "https://google.com", s.OriginalURL) | ||
assert.Equal(t, "user", s.CreatedBy) | ||
assert.Equal(t, int64(0), s.Visits) | ||
}) | ||
|
||
t.Run("returns 404 if shortening with given identifier does not exist", func(t *testing.T) { | ||
var ( | ||
provider = shorten.NewService(shortening.NewInMemory()) | ||
handler = server.HandleStats(provider) | ||
recorder = httptest.NewRecorder() | ||
request = httptest.NewRequest("GET", "/stats/abc", nil) | ||
e = echo.New() | ||
c = e.NewContext(request, recorder) | ||
) | ||
|
||
addUserToCtx(c) | ||
|
||
c.SetPath("/stats/:identifier") | ||
c.SetParamNames("identifier") | ||
c.SetParamValues("abc") | ||
|
||
var httpErr *echo.HTTPError | ||
require.ErrorAs(t, handler(c), &httpErr) | ||
assert.Equal(t, http.StatusNotFound, httpErr.Code) | ||
}) | ||
} |