tct/main_test.go

63 lines
1.3 KiB
Go
Raw Normal View History

2024-06-13 08:10:41 +00:00
package main
import (
"bytes"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/mock"
)
type MockHttpClient struct {
mock.Mock
}
func (m *MockHttpClient) Get(url string) (*http.Response, error) {
args := m.Called(url)
return args.Get(0).(*http.Response), args.Error(1)
}
func TestMakeRequest(t *testing.T) {
tests := []struct {
name string
statusCode int
responseBody string
expectedError error
}{
{
name: "Successful request",
statusCode: http.StatusOK,
responseBody: "OK",
},
{
name: "Failed request",
statusCode: http.StatusInternalServerError,
responseBody: "",
expectedError: errors.New("request failed"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockClient := new(MockHttpClient)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tt.statusCode)
w.Write([]byte(tt.responseBody))
}))
defer server.Close()
mockClient.On("Get", server.URL).Return(&http.Response{
StatusCode: tt.statusCode,
Body: io.NopCloser(bytes.NewBufferString(tt.responseBody)),
}, tt.expectedError)
makeRequest(mockClient, server.URL)
mockClient.AssertExpectations(t)
})
}
}