-
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.
chore: Improved testing with more coverage (#16)
- Loading branch information
1 parent
718d51f
commit 8e8f9f7
Showing
8 changed files
with
429 additions
and
38 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_StringEnumValue(t *testing.T) { | ||
t.Run("only allows specified values", func(t *testing.T) { | ||
sev := NewStringEnumValue([]string{"foo", "bar"}, "foo") | ||
expected := &StringEnumValue{ | ||
allowed: []string{"foo", "bar"}, | ||
value: "foo", | ||
} | ||
assert.Equal(t, expected, sev) | ||
|
||
err := sev.Set("baz") | ||
assert.ErrorContains(t, err, "baz is not an allowed value") | ||
|
||
err = sev.Set("bar") | ||
assert.Nil(t, err) | ||
expected.value = "bar" | ||
assert.Equal(t, expected, sev) | ||
}) | ||
|
||
t.Run("supports interface methods", func(t *testing.T) { | ||
sev := NewStringEnumValue([]string{"foo", "bar"}, "foo") | ||
assert.Equal(t, "string", sev.Type()) | ||
assert.Equal(t, "foo", sev.String()) | ||
|
||
err := sev.Set("bar") | ||
assert.Nil(t, err) | ||
assert.Equal(t, "bar", sev.String()) | ||
}) | ||
} |
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,145 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"github.com/jsumners/go-rfc3339" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
type RequestErrorTripper struct{} | ||
|
||
func (ret *RequestErrorTripper) RoundTrip(*http.Request) (*http.Response, error) { | ||
return nil, errors.New("bad request") | ||
} | ||
|
||
func Test_WithBaseUrl(t *testing.T) { | ||
npm := NewNpmClient(WithBaseUrl("http://127.0.0.1/")) | ||
assert.Equal(t, "http://127.0.0.1", npm.baseUrl) | ||
} | ||
|
||
func Test_GetDetailedInfo(t *testing.T) { | ||
t.Run("returns error for bad request construction", func(t *testing.T) { | ||
npm := NewNpmClient(WithBaseUrl("http://127.0.0.1")) | ||
result, err := npm.GetDetailedInfo("foo#%0x24") | ||
assert.Empty(t, result) | ||
assert.ErrorContains(t, err, "invalid URL escape") | ||
}) | ||
|
||
t.Run("returns error for server error", func(t *testing.T) { | ||
client := &http.Client{ | ||
Transport: &RequestErrorTripper{}, | ||
} | ||
npm := NewNpmClient(WithBaseUrl("http://127.0.0.1"), WithHttpClient(client)) | ||
|
||
result, err := npm.GetDetailedInfo("anything") | ||
assert.Empty(t, result) | ||
assert.ErrorContains(t, err, "bad request") | ||
}) | ||
|
||
t.Run("returns error for bad payload", func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
io.WriteString(res, `{"foo":"bar"`) | ||
})) | ||
npm := NewNpmClient(WithBaseUrl(ts.URL)) | ||
|
||
result, err := npm.GetDetailedInfo("anything") | ||
assert.Empty(t, result) | ||
assert.ErrorContains(t, err, "unexpected EOF") | ||
}) | ||
|
||
t.Run("handles error codes", func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
res.WriteHeader(500) | ||
io.WriteString(res, `failed`) | ||
})) | ||
npm := NewNpmClient(WithBaseUrl(ts.URL)) | ||
|
||
result, err := npm.GetDetailedInfo("anything") | ||
assert.Empty(t, result) | ||
assert.ErrorContains(t, err, "expected response code 200 but got 500: 500") | ||
}) | ||
|
||
t.Run("returns a success response", func(t *testing.T) { | ||
payload := `{ | ||
"versions": { | ||
"1.0.0": {} | ||
}, | ||
"time": { | ||
"1.0.0": "2024-05-03T13:00:00.000-04:00" | ||
} | ||
}` | ||
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
io.WriteString(res, payload) | ||
})) | ||
npm := NewNpmClient(WithBaseUrl(ts.URL)) | ||
|
||
dt, _ := rfc3339.NewDateTimeFromString("2024-05-03T13:00:00.000-04:00") | ||
expected := &NpmDetailedPackage{ | ||
Versions: map[string]any{"1.0.0": make(map[string]any)}, | ||
Time: map[string]rfc3339.DateTime{ | ||
"1.0.0": dt, | ||
}, | ||
} | ||
result, err := npm.GetDetailedInfo("anything") | ||
assert.Nil(t, err) | ||
assert.Equal(t, expected, result) | ||
}) | ||
} | ||
|
||
func Test_GetLatest(t *testing.T) { | ||
t.Run("returns error for bad request construction", func(t *testing.T) { | ||
npm := NewNpmClient(WithBaseUrl("http://127.0.0.1")) | ||
result, err := npm.GetLatest("foo#%0x24") | ||
assert.Empty(t, result) | ||
assert.ErrorContains(t, err, "invalid URL escape") | ||
}) | ||
|
||
t.Run("returns error for server error", func(t *testing.T) { | ||
client := &http.Client{ | ||
Transport: &RequestErrorTripper{}, | ||
} | ||
npm := NewNpmClient(WithBaseUrl("http://127.0.0.1"), WithHttpClient(client)) | ||
|
||
result, err := npm.GetLatest("anything") | ||
assert.Empty(t, result) | ||
assert.ErrorContains(t, err, "bad request") | ||
}) | ||
|
||
t.Run("returns error for bad payload", func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
io.WriteString(res, `{"foo":"bar"`) | ||
})) | ||
npm := NewNpmClient(WithBaseUrl(ts.URL)) | ||
|
||
result, err := npm.GetLatest("anything") | ||
assert.Empty(t, result) | ||
assert.ErrorContains(t, err, "unexpected EOF") | ||
}) | ||
|
||
t.Run("handles error codes", func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
res.WriteHeader(500) | ||
io.WriteString(res, `failed`) | ||
})) | ||
npm := NewNpmClient(WithBaseUrl(ts.URL)) | ||
|
||
result, err := npm.GetLatest("anything") | ||
assert.Empty(t, result) | ||
assert.ErrorContains(t, err, "expected response code 200 but got 500: 500") | ||
}) | ||
|
||
t.Run("returns a success response", func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
io.WriteString(res, `{"version":"1.0.0"}`) | ||
})) | ||
npm := NewNpmClient(WithBaseUrl(ts.URL)) | ||
|
||
result, err := npm.GetLatest("anything") | ||
assert.Nil(t, err) | ||
assert.Equal(t, "1.0.0", result) | ||
}) | ||
} |
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
Oops, something went wrong.