github.com/xmidt-org/webpa-common@v1.11.9/resource/loader_test.go (about)

     1  package resource
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  	"github.com/stretchr/testify/assert"
     8  	"net/http"
     9  	"sort"
    10  	"testing"
    11  )
    12  
    13  func TestData(t *testing.T) {
    14  	assert := assert.New(t)
    15  
    16  	message := "here are some resource bytes"
    17  	var loader Loader = &Data{
    18  		Source: []byte(message),
    19  	}
    20  
    21  	assert.Equal(message, loader.Location())
    22  	assert.Equal(message, fmt.Sprintf("%s", loader))
    23  
    24  	if read, err := ReadAll(loader); assert.Nil(err) {
    25  		assert.Equal(message, string(read))
    26  	}
    27  
    28  	// UseClient should have no effect
    29  	UseClient(loader, &http.Client{})
    30  
    31  	assert.Equal(message, loader.Location())
    32  	assert.Equal(message, fmt.Sprintf("%s", loader))
    33  
    34  	if read, err := ReadAll(loader); assert.Nil(err) {
    35  		assert.Equal(message, string(read))
    36  	}
    37  }
    38  
    39  func TestFile(t *testing.T) {
    40  	assert := assert.New(t)
    41  
    42  	var loader Loader = &File{
    43  		Path: testFilePath,
    44  	}
    45  
    46  	assert.Equal(testFilePath, loader.Location())
    47  	assert.Equal(testFilePath, fmt.Sprintf("%s", loader))
    48  
    49  	if read, err := ReadAll(loader); assert.Nil(err) {
    50  		assert.Equal(testContents, string(read))
    51  	}
    52  
    53  	// UseClient should have no effect
    54  	UseClient(loader, &http.Client{})
    55  
    56  	assert.Equal(testFilePath, loader.Location())
    57  	assert.Equal(testFilePath, fmt.Sprintf("%s", loader))
    58  
    59  	if read, err := ReadAll(loader); assert.Nil(err) {
    60  		assert.Equal(testContents, string(read))
    61  	}
    62  }
    63  
    64  func TestFileMissing(t *testing.T) {
    65  	assert := assert.New(t)
    66  	missingFile := "/this/file/does/not/exist.txt"
    67  	var loader Loader = &File{
    68  		Path: missingFile,
    69  	}
    70  
    71  	assert.Equal(missingFile, loader.Location())
    72  	assert.Equal(missingFile, fmt.Sprintf("%s", loader))
    73  
    74  	read, err := ReadAll(loader)
    75  	assert.Empty(read)
    76  	assert.NotNil(err)
    77  
    78  	// UseClient should have no effect
    79  	UseClient(loader, &http.Client{})
    80  
    81  	assert.Equal(missingFile, loader.Location())
    82  	assert.Equal(missingFile, fmt.Sprintf("%s", loader))
    83  
    84  	read, err = ReadAll(loader)
    85  	assert.Empty(read)
    86  	assert.NotNil(err)
    87  }
    88  
    89  func TestHTTPSimple(t *testing.T) {
    90  	assert := assert.New(t)
    91  
    92  	var loader Loader = &HTTP{
    93  		URL: testFileURL,
    94  	}
    95  
    96  	assert.Equal(testFileURL, loader.Location())
    97  	assert.Equal(testFileURL, fmt.Sprintf("%s", loader))
    98  
    99  	if read, err := ReadAll(loader); assert.Nil(err) {
   100  		assert.Equal(testContents, string(read))
   101  	}
   102  
   103  	newClientUsed := false
   104  	newClient := &testHTTPClient{
   105  		transport: func(request *http.Request) (*http.Response, error) {
   106  			newClientUsed = true
   107  			return http.DefaultClient.Do(request)
   108  		},
   109  	}
   110  
   111  	UseClient(loader, newClient)
   112  
   113  	assert.Equal(testFileURL, loader.Location())
   114  	assert.Equal(testFileURL, fmt.Sprintf("%s", loader))
   115  
   116  	if read, err := ReadAll(loader); assert.Nil(err) {
   117  		assert.Equal(testContents, string(read))
   118  	}
   119  
   120  	assert.True(newClientUsed)
   121  }
   122  
   123  func TestHTTPInvalidRequest(t *testing.T) {
   124  	assert := assert.New(t)
   125  
   126  	var loader Loader = &HTTP{
   127  		URL:    testFileURL,
   128  		Method: "INVALID METHOD",
   129  	}
   130  
   131  	data, err := ReadAll(loader)
   132  	assert.Len(data, 0)
   133  	assert.NotNil(err)
   134  }
   135  
   136  func TestHTTP(t *testing.T) {
   137  	assert := assert.New(t)
   138  
   139  	var testData = []struct {
   140  		statusCode  int
   141  		header      http.Header
   142  		method      string
   143  		clientError error
   144  		expectError bool
   145  	}{
   146  		{
   147  			statusCode: 200,
   148  		},
   149  		{
   150  			statusCode: 204,
   151  			header: http.Header{
   152  				"Accept": []string{"text/plain"},
   153  			},
   154  		},
   155  		{
   156  			statusCode:  404,
   157  			expectError: true,
   158  		},
   159  		{
   160  			clientError: errors.New("here is a nasty little error!"),
   161  			expectError: true,
   162  		},
   163  	}
   164  
   165  	for _, record := range testData {
   166  		t.Logf("%#v", record)
   167  		var body *readCloser
   168  
   169  		client := &testHTTPClient{
   170  			transport: func(request *http.Request) (*http.Response, error) {
   171  				if len(record.method) == 0 {
   172  					assert.Equal(DefaultMethod, request.Method)
   173  				} else {
   174  					assert.Equal(record.method, request.Method)
   175  				}
   176  
   177  				if assert.Len(request.Header, len(record.header)) {
   178  					for key, actualValues := range request.Header {
   179  						expectedValues := record.header[key]
   180  						if assert.Len(actualValues, len(expectedValues)) && len(expectedValues) > 0 {
   181  							sort.Strings(actualValues)
   182  							sort.Strings(expectedValues)
   183  							assert.Equal(expectedValues, actualValues)
   184  						}
   185  					}
   186  				}
   187  
   188  				if record.clientError != nil {
   189  					return nil, record.clientError
   190  				}
   191  
   192  				body := &readCloser{
   193  					reader: bytes.NewReader([]byte(testContents)),
   194  				}
   195  
   196  				return &http.Response{
   197  					StatusCode: record.statusCode,
   198  					Body:       body,
   199  				}, nil
   200  			},
   201  		}
   202  
   203  		var loader Loader = &HTTP{
   204  			URL:        testFileURL,
   205  			Header:     record.header,
   206  			Method:     record.method,
   207  			HTTPClient: client,
   208  		}
   209  
   210  		read, err := ReadAll(loader)
   211  		if record.expectError {
   212  			assert.Len(read, 0)
   213  			assert.NotNil(err)
   214  		} else {
   215  			assert.Equal(testContents, string(read))
   216  			assert.Nil(err)
   217  		}
   218  
   219  		assert.True(body == nil || body.closed)
   220  	}
   221  }