Welcome to mirror list, hosted at ThFree Co, Russian Federation.

resource_test.go « httprange « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1d6481fcabe45266a3f16bdf2b7343fae252b0a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package httprange

import (
	"context"
	"net/http"
	"net/http/httptest"
	"sync/atomic"
	"testing"

	"github.com/stretchr/testify/require"
)

func urlValue(url string) atomic.Value {
	v := atomic.Value{}
	v.Store(url)
	return v
}

func TestNewResource(t *testing.T) {
	resource := &Resource{
		url:          urlValue("/some/resource"),
		ETag:         "etag",
		LastModified: "Wed, 21 Oct 2015 07:28:00 GMT",
		Size:         1,
	}

	tests := map[string]struct {
		url            string
		status         int
		contentRange   string
		want           *Resource
		expectedErrMsg string
	}{
		"status_ok": {
			url:    "/some/resource",
			status: http.StatusOK,
			want:   resource,
		},
		"status_partial_content_success": {
			url:          "/some/resource",
			status:       http.StatusPartialContent,
			contentRange: "bytes 200-1000/67589",
			want: &Resource{
				url:          urlValue("/some/resource"),
				ETag:         "etag",
				LastModified: "Wed, 21 Oct 2015 07:28:00 GMT",
				Size:         67589,
			},
		},
		"status_partial_content_invalid_content_range": {
			url:            "/some/resource",
			status:         http.StatusPartialContent,
			contentRange:   "invalid",
			expectedErrMsg: "invalid `Content-Range`:",
			want:           resource,
		},
		"status_partial_content_content_range_not_a_number": {
			url:            "/some/resource",
			status:         http.StatusPartialContent,
			contentRange:   "bytes 200-1000/notanumber",
			expectedErrMsg: "invalid `Content-Range`:",
			want:           resource,
		},
		"StatusRequestedRangeNotSatisfiable": {
			url:            "/some/resource",
			status:         http.StatusRequestedRangeNotSatisfiable,
			expectedErrMsg: ErrRangeRequestsNotSupported.Error(),
			want:           resource,
		},
		"not_found": {
			url:            "/some/resource",
			status:         http.StatusNotFound,
			expectedErrMsg: ErrNotFound.Error(),
			want:           resource,
		},
		"invalid_url": {
			url:            "/%",
			expectedErrMsg: "invalid URL escape",
			want:           resource,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				w.Header().Set("ETag", tt.want.ETag)
				w.Header().Set("Last-Modified", tt.want.LastModified)
				w.Header().Set("Content-Range", tt.contentRange)
				w.WriteHeader(tt.status)
				w.Write([]byte("1"))
			}))
			defer testServer.Close()

			got, err := NewResource(context.Background(), testServer.URL+tt.url)
			if tt.expectedErrMsg != "" {
				require.Error(t, err)
				require.Contains(t, err.Error(), tt.expectedErrMsg)
				return
			}

			require.NoError(t, err)
			require.Contains(t, got.URL(), tt.want.URL())
			require.Equal(t, tt.want.LastModified, got.LastModified)
			require.Equal(t, tt.want.ETag, got.ETag)
			require.Equal(t, tt.want.Size, got.Size)
		})
	}
}