Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
onrik committed Aug 12, 2024
1 parent e2a54f3 commit 26172ed
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
go install github.com/mattn/goveralls@latest
$(go env GOPATH)/bin/goveralls -coverprofile=profile.cov -service=github -ignore=examples/*/*
goveralls -coverprofile=profile.cov -service=github -ignore=examples/*/*
2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type HTTPError struct {
}

func (e HTTPError) Error() string {
return fmt.Sprintf("HTTP status: %d", e.StatusCode)
return fmt.Sprintf("http status %d (%s)", e.StatusCode, http.StatusText(e.StatusCode))
}

type fileField struct {
Expand Down
31 changes: 31 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package micha

import (
"bytes"
"io"
"net/http"
"testing"

"github.com/stretchr/testify/require"
)

func TestHTTPError(t *testing.T) {
var err error = HTTPError{
StatusCode: http.StatusNotFound,
}

require.Equal(t, "http status 404 (Not Found)", err.Error())
}

func TestHandleResponse(t *testing.T) {
response := &http.Response{
Body: io.NopCloser(bytes.NewBuffer(nil)),
StatusCode: http.StatusForbidden,
}

body, err := handleResponse(response)
require.NotNil(t, err)
require.Nil(t, body)

require.Equal(t, "http status 403 (Forbidden)", err.Error())
}
23 changes: 23 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package micha

import (
"context"
"testing"

"github.com/stretchr/testify/require"
)

func TestWithAPIServer(t *testing.T) {
options := &Options{}
WithAPIServer("http://127.0.0.1/")(options)

require.Equal(t, "http://127.0.0.1", options.apiServer)
}

func TestWithCtx(t *testing.T) {
options := &Options{}
ctx := context.WithValue(context.Background(), "foo", "bar")
WithCtx(ctx)(options)

require.Equal(t, ctx, options.ctx)
}

0 comments on commit 26172ed

Please sign in to comment.