Skip to content

Commit

Permalink
RF: use testify in all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kynrai committed Jun 10, 2024
1 parent 38d31e2 commit 07b82d3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 50 deletions.
45 changes: 12 additions & 33 deletions handlers/cookies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,24 @@ package handlers
import (
"net/http"
"net/http/httptest"
"strings"
"testing"

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

func TestHandlerCookies(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
url string
expectedCode int
expectedError string
}{
{
name: "Missing URL",
url: "",
expectedCode: http.StatusBadRequest,
},
{
name: "Invalid URL",
url: "invalid_url",
expectedCode: http.StatusInternalServerError,
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
req := httptest.NewRequest("GET", "/cookies?url="+tc.url, nil)
rec := httptest.NewRecorder()
HandleCookies().ServeHTTP(rec, req)
t.Run("missing URL parameter", func(t *testing.T) {
t.Parallel()
req := httptest.NewRequest(http.MethodGet, "/blocklists", nil)
rec := httptest.NewRecorder()

HandleCookies().ServeHTTP(rec, req)

if rec.Code != tc.expectedCode {
t.Errorf("Expected status code %d, got %d", tc.expectedCode, rec.Code)
}
assert.Equal(t, http.StatusBadRequest, rec.Code)
assert.JSONEq(t, `{"error": "missing URL parameter"}`, rec.Body.String())
})

if tc.expectedError != "" && !strings.Contains(rec.Body.String(), tc.expectedError) {
t.Errorf("Expected error message '%s' not found in response body", tc.expectedError)
}
})
}
// TODO: Add tests for cookie logic
}
12 changes: 4 additions & 8 deletions handlers/dns_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package handlers
import (
"net/http"
"net/http/httptest"
"strings"
"testing"

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

func TestHandleDNSServer(t *testing.T) {
Expand Down Expand Up @@ -38,13 +39,8 @@ func TestHandleDNSServer(t *testing.T) {

HandleDNSServer().ServeHTTP(rec, req)

if rec.Code != tc.expectedCode {
t.Errorf("Expected status code %d, got %d", tc.expectedCode, rec.Code)
}

if strings.TrimSpace(rec.Body.String()) != tc.expectedBody {
t.Errorf("Expected body '%s', got '%s'", tc.expectedBody, strings.TrimSpace(rec.Body.String()))
}
assert.Equal(t, tc.expectedCode, rec.Code)
assert.JSONEq(t, tc.expectedBody, rec.Body.String())
})
}
}
6 changes: 3 additions & 3 deletions handlers/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"net/http"
"net/http/httptest"
"testing"

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

func TestHandleDNS(t *testing.T) {
Expand Down Expand Up @@ -34,9 +36,7 @@ func TestHandleDNS(t *testing.T) {

HandleDNS().ServeHTTP(rec, req)

if rec.Code != tc.expectedCode {
t.Errorf("Expected status code %d, got %d", tc.expectedCode, rec.Code)
}
assert.Equal(t, tc.expectedCode, rec.Code)
})
}
}
6 changes: 3 additions & 3 deletions handlers/dnssec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"net/http"
"net/http/httptest"
"testing"

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

func TestHandleDnsSec(t *testing.T) {
Expand Down Expand Up @@ -34,9 +36,7 @@ func TestHandleDnsSec(t *testing.T) {

HandleDnsSec().ServeHTTP(rec, req)

if rec.Code != tc.expectedCode {
t.Errorf("Expected status code %d, got %d", tc.expectedCode, rec.Code)
}
assert.Equal(t, tc.expectedCode, rec.Code)
})
}
}
6 changes: 3 additions & 3 deletions handlers/firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"net/http"
"net/http/httptest"
"testing"

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

func TestHandleFirewall(t *testing.T) {
Expand Down Expand Up @@ -34,9 +36,7 @@ func TestHandleFirewall(t *testing.T) {

HandleFirewall().ServeHTTP(rec, req)

if rec.Code != tc.expectedCode {
t.Errorf("Expected status code %d, got %d", tc.expectedCode, rec.Code)
}
assert.Equal(t, tc.expectedCode, rec.Code)
})
}
}
1 change: 1 addition & 0 deletions handlers/redirects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestHandleGetRedirects(t *testing.T) {
var response map[string]interface{}
err := json.Unmarshal(rec.Body.Bytes(), &response)
assert.NoError(t, err)
// TODO: break this out of table drive tests, should not use name as part of logic
if tc.name == "Invalid URL" {
assert.Contains(t, response["error"], tc.expectedBody["error"])
} else {
Expand Down

0 comments on commit 07b82d3

Please sign in to comment.