Initial commit
This commit is contained in:
39
internal/server/routes_test.go
Normal file
39
internal/server/routes_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHandler(t *testing.T) {
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
resp := httptest.NewRecorder()
|
||||
c := e.NewContext(req, resp)
|
||||
s := &Server{}
|
||||
// Assertions
|
||||
if err := s.HelloWorldHandler(c); err != nil {
|
||||
t.Errorf("handler() error = %v", err)
|
||||
return
|
||||
}
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Errorf("handler() wrong status code = %v", resp.Code)
|
||||
return
|
||||
}
|
||||
expected := map[string]string{"message": "Hello World"}
|
||||
var actual map[string]string
|
||||
// Decode the response body into the actual map
|
||||
if err := json.NewDecoder(resp.Body).Decode(&actual); err != nil {
|
||||
t.Errorf("handler() error decoding response body: %v", err)
|
||||
return
|
||||
}
|
||||
// Compare the decoded response with the expected value
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Errorf("handler() wrong response body. expected = %v, actual = %v", expected, actual)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user