Skip to content

Commit

Permalink
Merge pull request #21 from hashmap-kz/ci/checklint
Browse files Browse the repository at this point in the history
checklint
  • Loading branch information
hashmap-kz authored Jan 7, 2025
2 parents 823c549 + 5566dd5 commit bc13a52
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 45 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ jobs:
with:
go-version-file: go.mod

- name: Install golangci-lint
- name: Run golangci-lint
if: runner.os == 'Linux'
uses: golangci/golangci-lint-action@v6
with:
version: v1.62.2
skip-cache: true

- name: Lint
if: runner.os == 'Linux'
run: make lint
106 changes: 66 additions & 40 deletions pkg/cmd/remote_test.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,77 @@
package cmd

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

func TestReadRemote(t *testing.T) {
t.Run("Successful Request", func(t *testing.T) {
mockHTTPResponse := "Remote file content"
http.DefaultClient = &http.Client{
Transport: roundTripper(func(_ *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(mockHTTPResponse)),
}
}),
}
result, err := ReadRemoteFileContent("http://example.com/data")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if string(result) != mockHTTPResponse {
t.Errorf("Expected: %s, Got: %s", mockHTTPResponse, string(result))
}
})
func TestReadRemoteFileContent(t *testing.T) {
tests := []struct {
name string
inputURL string
mockResponse string
statusCode int
wantError bool
}{
{
name: "Valid URL and HTTP response",
inputURL: "http://example.com/test",
mockResponse: "file content",
statusCode: http.StatusOK,
wantError: false,
},
{
name: "Invalid URL - malformed",
inputURL: ":invalid-url",
wantError: true,
},
{
name: "Invalid URL - missing host",
inputURL: "http:///test",
wantError: true,
},
{
name: "Non-200 HTTP status code",
inputURL: "http://example.com/notfound",
statusCode: http.StatusNotFound,
wantError: true,
},
}

t.Run("Failed Request", func(t *testing.T) {
http.DefaultClient = &http.Client{
Transport: roundTripper(func(_ *http.Request) *http.Response {
return &http.Response{
StatusCode: 404,
Body: io.NopCloser(strings.NewReader("Not Found")),
}
}),
}
_, err := ReadRemoteFileContent("http://example.com/not-found")
if err == nil {
t.Error("Expected error but got none")
}
})
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a mock HTTP server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(tt.statusCode)
fmt.Fprint(w, tt.mockResponse)
}))
defer server.Close()

// Replace the input URL with the mock server's URL if statusCode is set
inputURL := tt.inputURL
if tt.statusCode != 0 {
inputURL = server.URL
}

// Helper for mocking http.Client
type roundTripper func(req *http.Request) *http.Response
// Call the function under test
got, err := ReadRemoteFileContent(inputURL)

func (f roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
// Check for errors
if tt.wantError {
if err == nil {
t.Errorf("Expected an error but got none")
}
} else {
if err != nil {
t.Errorf("Did not expect an error but got one: %v", err)
}
// Verify the response content
if string(got) != tt.mockResponse {
t.Errorf("Expected response %q, got %q", tt.mockResponse, got)
}
}
})
}
}

0 comments on commit bc13a52

Please sign in to comment.