github.com/adevinta/lava@v0.7.2/internal/urlutil/urlutil_test.go (about)

     1  // Copyright 2023 Adevinta
     2  
     3  package urlutil
     4  
     5  import (
     6  	"errors"
     7  	"fmt"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/google/go-cmp/cmp"
    14  )
    15  
    16  func TestGet_HTTP(t *testing.T) {
    17  	tests := []struct {
    18  		name        string
    19  		handlerFunc func(http.ResponseWriter, *http.Request)
    20  		want        []byte
    21  		wantNilErr  bool
    22  	}{
    23  		{
    24  			name: "valid",
    25  			handlerFunc: func(writer http.ResponseWriter, request *http.Request) {
    26  				fmt.Fprintln(writer, "response body")
    27  			},
    28  			want:       []byte("response body\n"),
    29  			wantNilErr: true,
    30  		},
    31  		{
    32  			name:        "empty",
    33  			handlerFunc: func(writer http.ResponseWriter, request *http.Request) {},
    34  			want:        []byte{},
    35  			wantNilErr:  true,
    36  		},
    37  		{
    38  			name: "not found",
    39  			handlerFunc: func(writer http.ResponseWriter, request *http.Request) {
    40  				http.Error(writer, "not found", http.StatusNotFound)
    41  			},
    42  			want:       nil,
    43  			wantNilErr: false,
    44  		},
    45  	}
    46  
    47  	for _, tt := range tests {
    48  		t.Run(tt.name, func(t *testing.T) {
    49  			ts := httptest.NewServer(http.HandlerFunc(tt.handlerFunc))
    50  			defer ts.Close()
    51  
    52  			got, err := Get(ts.URL)
    53  			if (err == nil) != tt.wantNilErr {
    54  				t.Fatalf("unexpected error: want nil: %v, got: %v", tt.wantNilErr, err)
    55  			}
    56  
    57  			if diff := cmp.Diff(tt.want, got); diff != "" {
    58  				t.Errorf("content mismatch (-want +got):\n%v", diff)
    59  			}
    60  		})
    61  	}
    62  }
    63  
    64  func TestGet_URL(t *testing.T) {
    65  	tests := []struct {
    66  		name    string
    67  		url     string
    68  		want    []byte
    69  		wantErr error
    70  	}{
    71  		{
    72  			name:    "file",
    73  			url:     "testdata/content.txt",
    74  			want:    []byte("file with content\n"),
    75  			wantErr: nil,
    76  		},
    77  		{
    78  			name:    "empty file",
    79  			url:     "testdata/empty.txt",
    80  			want:    []byte{},
    81  			wantErr: nil,
    82  		},
    83  		{
    84  			name:    "file does not exist",
    85  			url:     "testdata/not_exist",
    86  			want:    nil,
    87  			wantErr: os.ErrNotExist,
    88  		},
    89  		{
    90  			name:    "empty file path",
    91  			url:     "",
    92  			want:    nil,
    93  			wantErr: os.ErrNotExist,
    94  		},
    95  		{
    96  			name:    "invalid scheme",
    97  			url:     "invalid://example.com/file.json",
    98  			want:    nil,
    99  			wantErr: ErrInvalidScheme,
   100  		},
   101  		{
   102  			name:    "invalid URL",
   103  			url:     "1http://example.com/file.json",
   104  			want:    nil,
   105  			wantErr: ErrInvalidURL,
   106  		},
   107  	}
   108  
   109  	for _, tt := range tests {
   110  		t.Run(tt.name, func(t *testing.T) {
   111  			got, err := Get(tt.url)
   112  			if !errors.Is(err, tt.wantErr) {
   113  				t.Errorf("unexpected error: want: %v, got: %v", tt.wantErr, err)
   114  			}
   115  
   116  			if diff := cmp.Diff(tt.want, got); diff != "" {
   117  				t.Errorf("content mismatch (-want +got):\n%v", diff)
   118  			}
   119  		})
   120  	}
   121  }