From a0955a4f6b2606e43ee0fdbefb239dd9ec5438e8 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Thu, 13 Jun 2024 11:10:41 +0300 Subject: [PATCH] unit tests --- go.mod | 9 ++++++++ go.sum | 12 ++++++++++ main.go | 15 ++++++++++--- main_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 go.sum create mode 100644 main_test.go diff --git a/go.mod b/go.mod index 9ec14d2..8bff24f 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,12 @@ module notashelf.dev/tct go 1.22.2 + +require github.com/stretchr/testify v1.9.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.5.2 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8565daa --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index b58b55e..25c69aa 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "context" "flag" "fmt" + "io" "net/http" "os" "os/signal" @@ -12,6 +13,12 @@ import ( "time" ) +type HttpClient interface { + Get(url string) (*http.Response, error) +} + +var httpClient HttpClient = &http.Client{} + func main() { urlPtr := flag.String("url", "http://example.com", "URL to fetch") maxRequestsPtr := flag.Int("max", 100, "Maximum number of parallel requests") @@ -45,7 +52,7 @@ func main() { return default: time.Sleep(delay) - makeRequest(url) + makeRequest(httpClient, url) } }() } @@ -60,11 +67,13 @@ func main() { fmt.Printf("\nOptimal Number of Parallel TCP Requests: %d\n", optimal) } -func makeRequest(url string) { - resp, err := http.Get(url) +func makeRequest(client HttpClient, url string) { + resp, err := client.Get(url) if err != nil { fmt.Println(err) return } defer resp.Body.Close() + body, _ := io.ReadAll(resp.Body) + _ = body } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..b6186c5 --- /dev/null +++ b/main_test.go @@ -0,0 +1,62 @@ +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) + }) + } +}