github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/pkg/curl/schemes_test.go (about)

     1  // Copyright 2017-2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package curl
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  	"io/ioutil"
    11  	"net/url"
    12  	"testing"
    13  
    14  	"github.com/u-root/u-root/pkg/uio"
    15  )
    16  
    17  func TestFetch(t *testing.T) {
    18  	for i, tt := range []struct {
    19  		scheme func() *MockScheme
    20  		url    *url.URL
    21  		err    error
    22  		want   string
    23  	}{
    24  		{
    25  			scheme: func() *MockScheme {
    26  				s := NewMockScheme("fooftp")
    27  				s.Add("192.168.0.1", "/foo/pxelinux.cfg/default", "haha")
    28  				return s
    29  			},
    30  			want: "haha",
    31  			url: &url.URL{
    32  				Scheme: "fooftp",
    33  				Host:   "192.168.0.1",
    34  				Path:   "/foo/pxelinux.cfg/default",
    35  			},
    36  		},
    37  		{
    38  			scheme: func() *MockScheme {
    39  				return NewMockScheme("fooftp")
    40  			},
    41  			url: &url.URL{
    42  				Scheme: "nosuch",
    43  			},
    44  			err: ErrNoSuchScheme,
    45  		},
    46  		{
    47  			scheme: func() *MockScheme {
    48  				return NewMockScheme("fooftp")
    49  			},
    50  			url: &url.URL{
    51  				Scheme: "fooftp",
    52  				Host:   "someotherplace",
    53  			},
    54  			err: ErrNoSuchHost,
    55  		},
    56  		{
    57  			scheme: func() *MockScheme {
    58  				s := NewMockScheme("fooftp")
    59  				s.Add("somehost", "somefile", "somecontent")
    60  				return s
    61  			},
    62  			url: &url.URL{
    63  				Scheme: "fooftp",
    64  				Host:   "somehost",
    65  				Path:   "/someotherfile",
    66  			},
    67  			err: ErrNoSuchFile,
    68  		},
    69  	} {
    70  		t.Run(fmt.Sprintf("Test #%02d", i), func(t *testing.T) {
    71  			fs := tt.scheme()
    72  			s := make(Schemes)
    73  			s.Register(fs.Scheme, fs)
    74  
    75  			// Test both Fetch and LazyFetch.
    76  			for _, f := range []func(url *url.URL) (io.ReaderAt, error){
    77  				s.Fetch,
    78  				s.LazyFetch,
    79  			} {
    80  				r, err := f(tt.url)
    81  				if uErr, ok := err.(*URLError); ok && uErr.Err != tt.err {
    82  					t.Errorf("Fetch() = %v, want %v", uErr.Err, tt.err)
    83  				} else if !ok && err != tt.err {
    84  					t.Errorf("Fetch() = %v, want %v", err, tt.err)
    85  				}
    86  				if err != nil {
    87  					return
    88  				}
    89  				content, err := ioutil.ReadAll(uio.Reader(r))
    90  				if err != nil {
    91  					t.Errorf("bytes.Buffer read returned an error? %v", err)
    92  				}
    93  				if got, want := string(content), tt.want; got != want {
    94  					t.Errorf("Fetch() = %v, want %v", got, want)
    95  				}
    96  			}
    97  		})
    98  	}
    99  }