Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions checks/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -47,14 +48,9 @@ func TestInterpolateVariables(t *testing.T) {
func TestInterpolationNames(t *testing.T) {
got := InterpolationNames("${baseURL}/users/${id}/${id}")
want := []string{"baseURL", "id", "id"}
if len(got) != len(want) {
if !slices.Equal(got, want) {
t.Fatalf("InterpolationNames() = %#v, want %#v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("InterpolationNames() = %#v, want %#v", got, want)
}
}
}

func TestRunHTTPRequestInterpolatesRequestAndCapturesResponseVariables(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion checks/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func evaluateHeaderEquals(headers map[string]string, test api.HTTPRequestTestHea
if !ok {
return fmt.Errorf("expected %s %q to exist", label, key)
}
if !strings.EqualFold(got, want) {
if got != want {
return fmt.Errorf("expected %s %q to equal %q, got %q", label, key, want, got)
}

Expand Down
71 changes: 64 additions & 7 deletions checks/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

api "github.com/bootdotdev/bootdev/client"
"github.com/goccy/go-json"
)

func TestLocalSubmissionEventPassesCLIAndHTTPResults(t *testing.T) {
Expand Down Expand Up @@ -107,6 +108,68 @@ func TestEvaluateStdoutJq(t *testing.T) {
}
}

func TestEvaluateHTTPRequestTestsHeaderAndTrailerEquality(t *testing.T) {
tests := []struct {
name string
test api.HTTPRequestTest
result api.HTTPRequestResult
wantFailure bool
}{
{
name: "header name is case insensitive",
test: api.HTTPRequestTest{HeadersEqual: &api.HTTPRequestTestHeader{
Key: "X-Request-ID",
Value: "abc123",
}},
result: api.HTTPRequestResult{
ResponseHeaders: map[string]string{"x-request-id": "abc123"},
},
},
{
name: "header value is case sensitive",
test: api.HTTPRequestTest{HeadersEqual: &api.HTTPRequestTestHeader{
Key: "X-Request-ID",
Value: "abc123",
}},
result: api.HTTPRequestResult{
ResponseHeaders: map[string]string{"X-Request-ID": "ABC123"},
},
wantFailure: true,
},
{
name: "trailer name is case insensitive",
test: api.HTTPRequestTest{TrailersEqual: &api.HTTPRequestTestHeader{
Key: "X-Checksum",
Value: "sha256:abc",
}},
result: api.HTTPRequestResult{
ResponseTrailers: map[string]string{"x-checksum": "sha256:abc"},
},
},
{
name: "trailer value is case sensitive",
test: api.HTTPRequestTest{TrailersEqual: &api.HTTPRequestTestHeader{
Key: "X-Checksum",
Value: "sha256:abc",
}},
result: api.HTTPRequestResult{
ResponseTrailers: map[string]string{"X-Checksum": "SHA256:ABC"},
},
wantFailure: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
request := api.CLIStepHTTPRequest{Tests: []api.HTTPRequestTest{tt.test}}
failure := evaluateHTTPRequestTests(0, request, tt.result)
if (failure != nil) != tt.wantFailure {
t.Fatalf("failure = %#v, wantFailure = %t", failure, tt.wantFailure)
}
})
}
}

func TestLocalSubmissionEventRejectsMissingHTTPResponseCaptures(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -194,7 +257,7 @@ func TestValuesEqualPreservesTypes(t *testing.T) {
{name: "string and bool", got: "true", want: true, ok: false},
{name: "same bools", got: true, want: true, ok: true},
{name: "numeric int and float", got: 1, want: 1.0, ok: true},
{name: "numeric json number and int", got: testJSONNumber("1"), want: 1, ok: true},
{name: "numeric json number and int", got: json.Number("1"), want: 1, ok: true},
{name: "nil and string", got: nil, want: "<nil>", ok: false},
}

Expand All @@ -207,12 +270,6 @@ func TestValuesEqualPreservesTypes(t *testing.T) {
}
}

type testJSONNumber string

func (n testJSONNumber) String() string {
return string(n)
}

func intPtr(v int) *int {
return &v
}
Expand Down
16 changes: 6 additions & 10 deletions checks/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ func TestApplySubmissionResultsMarksAllStepsAndTestsPassedWhenNoFailure(t *testi
messages.ResolveTestMsg{StepIndex: 1, TestIndex: 0, Passed: boolPtr(true)},
}

assertMessages(t, got, want)
if !reflect.DeepEqual(got, want) {
t.Fatalf("messages = %#v, want %#v", got, want)
}
}

func TestApplySubmissionResultsStopsAfterFailedCLITest(t *testing.T) {
Expand All @@ -166,7 +168,9 @@ func TestApplySubmissionResultsStopsAfterFailedCLITest(t *testing.T) {
messages.ResolveTestMsg{StepIndex: 1, TestIndex: 1, Passed: boolPtr(false)},
}

assertMessages(t, got, want)
if !reflect.DeepEqual(got, want) {
t.Fatalf("messages = %#v, want %#v", got, want)
}
}

func applySubmissionResultsMessages(cliData api.CLIData, failure *api.StructuredErrCLI) []tea.Msg {
Expand All @@ -177,14 +181,6 @@ func applySubmissionResultsMessages(cliData api.CLIData, failure *api.Structured
return msgs
}

func assertMessages(t *testing.T, got []tea.Msg, want []tea.Msg) {
t.Helper()

if !reflect.DeepEqual(got, want) {
t.Fatalf("messages = %#v, want %#v", got, want)
}
}

func boolPtr(v bool) *bool {
return &v
}
5 changes: 5 additions & 0 deletions cmd/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func TestLoginHTTPHandlerRejectsMissingOrigin(t *testing.T) {
if response.Code != http.StatusForbidden {
t.Fatalf("status = %d, want %d", response.Code, http.StatusForbidden)
}
select {
case code := <-inputChan:
t.Fatalf("unexpected login code accepted: %q", code)
default:
}
}

func TestLoginHTTPHandlerLimitsCodeSize(t *testing.T) {
Expand Down