Skip to content

Commit

Permalink
added unit test for the search Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tanbirali committed Oct 7, 2024
1 parent a23bf92 commit 102bbe9
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions internal/tests/integration/apiSearchHandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func TestSearchHandler(t *testing.T) {
tests := []struct {
name string
query string
expectedStatus int
expectedBody string
}{
{
name: "Missing query parameter",
query: "",
expectedStatus: http.StatusBadRequest,
expectedBody: "Missing query parameter 'q'",
},
{
name: "Wildcard search",
query: "*",
expectedStatus: http.StatusOK,
expectedBody: "No search results",
},
{
name: "No matching results",
query: "nonexistent",
expectedStatus: http.StatusOK,
expectedBody: "No search results",
},
{
name: "Matching result",
query: "example",
expectedStatus: http.StatusOK,
expectedBody: "total",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

req := httptest.NewRequest("GET", "/search?q="+tt.query, nil)
rec := httptest.NewRecorder()


SearchHandler(rec, req)


if rec.Code != tt.expectedStatus {
t.Errorf("expected status %v, got %v", tt.expectedStatus, rec.Code)
}


respBody, err := ioutil.ReadAll(rec.Body)
if err != nil {
t.Fatalf("Failed to read response body: %v", err)
}
bodyString := string(respBody)


if !strings.Contains(bodyString, tt.expectedBody) {
t.Errorf("expected body to contain %q, got %q", tt.expectedBody, bodyString)
}
})
}
}

0 comments on commit 102bbe9

Please sign in to comment.