-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
367 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Build | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
build: | ||
name: Build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Set up Go 1.x | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: ^1.17 | ||
id: go | ||
|
||
- name: Set up linter | ||
run: go get -u github.com/mgechev/revive | ||
|
||
- name: Check out code into the Go module directory | ||
uses: actions/checkout@v2 | ||
|
||
- name: Get dependencies | ||
run: go get -v -t -d ./... | ||
|
||
- name: Lint | ||
run: revive -config revive.toml -formatter stylish -exclude ./vendor/... ./... | ||
|
||
- name: Vet | ||
run: go vet ./... | ||
|
||
- name: Test | ||
run: go test -v ./... |
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 |
---|---|---|
@@ -1,2 +1,31 @@ | ||
# waitfor-http | ||
HTTP resource readiness assertion library | ||
|
||
# Quick start | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/go-waitfor/waitfor" | ||
"github.com/go-waitfor/waitfor-http" | ||
"os" | ||
) | ||
|
||
func main() { | ||
runner := waitfor.New(http.Use()) | ||
|
||
err := runner.Test( | ||
context.Background(), | ||
[]string{"http://locahost:5432", "https://www.google.com"}, | ||
waitfor.WithAttempts(5), | ||
) | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
``` |
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,16 @@ | ||
module github.com/go-waitfor/waitfor-http | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/go-waitfor/waitfor v1.0.0 | ||
github.com/jarcoal/httpmock v1.0.8 | ||
github.com/stretchr/testify v1.7.0 | ||
) | ||
|
||
require ( | ||
github.com/cenkalti/backoff v2.2.1+incompatible // indirect | ||
github.com/davecgh/go-spew v1.1.0 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect | ||
) |
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,17 @@ | ||
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= | ||
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-waitfor/waitfor v1.0.0 h1:KX6SpTtEM2OOwJu5QP6MXvdi0Llq55d8dywSSZD+gSI= | ||
github.com/go-waitfor/waitfor v1.0.0/go.mod h1:a5e6B1hss5InR3moU7xAOP2thPsbjbTArD5+Kud4YaQ= | ||
github.com/jarcoal/httpmock v1.0.8 h1:8kI16SoO6LQKgPE7PvQuV+YuD/inwHd7fOOe2zMbo4k= | ||
github.com/jarcoal/httpmock v1.0.8/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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,59 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/go-waitfor/waitfor" | ||
) | ||
|
||
const Scheme = "http" | ||
|
||
type HTTP struct { | ||
url *url.URL | ||
} | ||
|
||
func Use() waitfor.ResourceConfig { | ||
return waitfor.ResourceConfig{ | ||
Scheme: []string{Scheme, Scheme + "s"}, | ||
Factory: New, | ||
} | ||
} | ||
|
||
func New(u *url.URL) (waitfor.Resource, error) { | ||
if u == nil { | ||
return nil, fmt.Errorf("%q: %w", "url", waitfor.ErrInvalidArgument) | ||
} | ||
|
||
return &HTTP{u}, nil | ||
} | ||
|
||
func (h *HTTP) Test(ctx context.Context) error { | ||
req, err := http.NewRequest(http.MethodGet, h.url.String(), nil) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
client := http.Client{} | ||
resp, err := client.Do(req.WithContext(ctx)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
if resp.Body != nil { | ||
_ = resp.Body.Close() | ||
} | ||
}() | ||
|
||
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest { | ||
return errors.New(resp.Status) | ||
} | ||
|
||
return err | ||
} |
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,107 @@ | ||
package http_test | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/go-waitfor/waitfor" | ||
"github.com/go-waitfor/waitfor-http" | ||
) | ||
|
||
func TestUse(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
uStr := "http://api.test.com/" | ||
|
||
httpmock.RegisterResponder("GET", uStr, | ||
httpmock.NewStringResponder(200, `OK`)) | ||
|
||
w := waitfor.New(http.Use()) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) | ||
defer cancel() | ||
err := w.Test(ctx, []string{uStr}) | ||
|
||
assert.NoError(t, err) | ||
} | ||
|
||
func TestHTTP(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
uStr := "http://api.test.com/" | ||
|
||
httpmock.RegisterResponder("GET", uStr, | ||
httpmock.NewStringResponder(200, `OK`)) | ||
|
||
u, err := url.Parse(uStr) | ||
|
||
assert.NoError(t, err) | ||
|
||
r, err := http.New(u) | ||
|
||
assert.NoError(t, err) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
err = r.Test(ctx) | ||
|
||
assert.NoError(t, err) | ||
} | ||
|
||
func TestHTTP_Failed(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
uStr := "http://foobar" | ||
|
||
httpmock.RegisterResponder("GET", uStr, | ||
httpmock.NewStringResponder(404, ``)) | ||
|
||
u, err := url.Parse(uStr) | ||
|
||
assert.NoError(t, err) | ||
|
||
r, err := http.New(u) | ||
|
||
assert.NoError(t, err) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
err = r.Test(ctx) | ||
|
||
assert.Error(t, err) | ||
} | ||
|
||
func TestHTTPS(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
uStr := "https://api.test.com/" | ||
|
||
httpmock.RegisterResponder("GET", uStr, | ||
httpmock.NewStringResponder(200, `OK`)) | ||
|
||
u, err := url.Parse(uStr) | ||
|
||
assert.NoError(t, err) | ||
|
||
r, err := http.New(u) | ||
|
||
assert.NoError(t, err) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
err = r.Test(ctx) | ||
|
||
assert.NoError(t, err) | ||
} |
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,26 @@ | ||
ignoreGeneratedHeader = true | ||
severity = "error" | ||
confidence = 0.8 | ||
errorCode = 1 | ||
warningCode = 0 | ||
|
||
[rule.context-as-argument] | ||
[rule.context-keys-type] | ||
[rule.dot-imports] | ||
[rule.error-return] | ||
[rule.error-strings] | ||
[rule.error-naming] | ||
[rule.if-return] | ||
[rule.increment-decrement] | ||
[rule.var-declaration] | ||
[rule.range] | ||
[rule.receiver-naming] | ||
[rule.time-naming] | ||
[rule.unexported-return] | ||
[rule.indent-error-flow] | ||
[rule.errorf] | ||
[rule.empty-block] | ||
[rule.superfluous-else] | ||
[rule.unused-parameter] | ||
[rule.unreachable-code] | ||
[rule.redefines-builtin-id] |