Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package httpstatuschecker
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type roundTripperFunc func(*http.Request) (*http.Response, error)
|
|
|
|
func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
return f(req)
|
|
}
|
|
|
|
func newTestClient(fn roundTripperFunc) *http.Client {
|
|
return &http.Client{Transport: fn}
|
|
}
|
|
|
|
func TestCheckStatus(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
returnedStatus int
|
|
isAvaiable bool
|
|
}{
|
|
{"200 OK", 200, true},
|
|
{"202 Accepted ", 202, true},
|
|
{"404 Not Found", 404, false},
|
|
}
|
|
for _, tt := range cases {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
client := newTestClient(func(req *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: tt.returnedStatus,
|
|
Body: io.NopCloser(bytes.NewReader(nil)),
|
|
Header: make(http.Header),
|
|
ContentLength: 0,
|
|
Request: req,
|
|
}, nil
|
|
})
|
|
|
|
isAvailable, err := Check(context.Background(), "http://example.com", Client(client))
|
|
require.NoError(t, err)
|
|
require.Equal(t, tt.isAvaiable, isAvailable)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCheckServerUnreachable(t *testing.T) {
|
|
client := newTestClient(func(*http.Request) (*http.Response, error) {
|
|
return nil, errors.New("dial tcp: connection refused")
|
|
})
|
|
isAvailable, err := Check(context.Background(), "http://example.com", Client(client))
|
|
require.NoError(t, err)
|
|
require.False(t, isAvailable)
|
|
}
|