github.com/saucelabs/saucectl@v0.175.1/internal/http/github_test.go (about)

     1  package http
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestGitHub_isUpdateRequired(t *testing.T) {
    11  	gh := GitHub{}
    12  
    13  	testCases := []struct {
    14  		current string
    15  		remote  string
    16  		want    bool
    17  	}{
    18  		{current: "v0.1.0", remote: "v0.1.1", want: true},
    19  		{current: "v0.2.0", remote: "v0.1.1", want: false},
    20  		{current: "v0.1.0", remote: "v0.1.0", want: false},
    21  		{current: "0.1.0", remote: "v0.1.1", want: true},
    22  		{current: "0.2.0", remote: "v0.1.1", want: false},
    23  		{current: "0.1.0", remote: "v0.1.0", want: false},
    24  		{current: "v0.1.0", remote: "0.1.1", want: true},
    25  		{current: "v0.2.0", remote: "0.1.1", want: false},
    26  		{current: "v0.1.0", remote: "0.1.0", want: false},
    27  		{current: "0.1.0", remote: "0.1.1", want: true},
    28  		{current: "0.2.0", remote: "0.1.1", want: false},
    29  		{current: "0.1.0", remote: "0.1.0", want: false},
    30  		{current: "v0.0.0+unknown", remote: "v0.1.0", want: true},
    31  	}
    32  	for _, tt := range testCases {
    33  		got := gh.isUpdateRequired(tt.current, tt.remote)
    34  		if tt.want != got {
    35  			t.Errorf("%s <=> %s, want: %v, got: %v", tt.current, tt.remote, tt.want, got)
    36  		}
    37  	}
    38  }
    39  
    40  func TestGitHub_IsUpdateAvailable(t *testing.T) {
    41  	testCases := []struct {
    42  		body    []byte
    43  		current string
    44  		want    string
    45  		wantErr error
    46  	}{
    47  		{
    48  			body:    []byte(`{"tag_name": "v0.43.0", "name": "v0.43.0"}`),
    49  			current: "v0.1.0",
    50  			want:    "v0.43.0",
    51  			wantErr: nil,
    52  		},
    53  		{
    54  			body:    []byte(`{"tag_name": "v0.43.0", "name": "v0.43.0"}`),
    55  			current: "v0.44.0",
    56  			want:    "",
    57  			wantErr: nil,
    58  		},
    59  		{
    60  			body:    []byte(`{"tag_name": "v0.43.0", "name": "v0.43.0"}`),
    61  			current: "v0.0.0+unknown",
    62  			want:    "v0.43.0",
    63  			wantErr: nil,
    64  		},
    65  	}
    66  	for idx, tt := range testCases {
    67  		ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    68  			var err error
    69  			switch r.URL.Path {
    70  			case "/repos/saucelabs/saucectl/releases/latest":
    71  				w.WriteHeader(200)
    72  				_, err = w.Write(tt.body)
    73  			default:
    74  				w.WriteHeader(http.StatusInternalServerError)
    75  			}
    76  
    77  			if err != nil {
    78  				t.Errorf("%d: failed to respond: %v", idx, err)
    79  			}
    80  		}))
    81  		gh := GitHub{
    82  			HTTPClient: &http.Client{Timeout: 1 * time.Second},
    83  			URL:        ts.URL,
    84  		}
    85  
    86  		// Forcing current version
    87  		got, err := gh.IsUpdateAvailable(tt.current)
    88  
    89  		if err != tt.wantErr {
    90  			t.Errorf("Case %d (err): want: %v, got: %v", idx, tt.wantErr, err)
    91  		}
    92  		if got != tt.want {
    93  			t.Errorf("Case %d: want: %v, got: %v", idx, tt.want, got)
    94  		}
    95  		ts.Close()
    96  	}
    97  }