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: ace8f92cdbf1c4aa715469c1aff33ba0ffeac167 (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
package httprange

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

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

func TestNewResource(t *testing.T) {
	resource := Resource{
		URL:          "/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: func() Resource {
				r := resource
				r.Size = 67589
				return r
			}(),
		},
		"status_partial_content_invalid_content_range": {
			url:            "/some/resource",
			status:         http.StatusPartialContent,
			contentRange:   "invalid",
			expectedErrMsg: "invalid `Content-Range`:",
		},
		"status_partial_content_content_range_not_a_number": {
			url:            "/some/resource",
			status:         http.StatusPartialContent,
			contentRange:   "bytes 200-1000/notanumber",
			expectedErrMsg: "invalid `Content-Range`:",
		},
		"StatusRequestedRangeNotSatisfiable": {
			url:            "/some/resource",
			status:         http.StatusRequestedRangeNotSatisfiable,
			expectedErrMsg: ErrRangeRequestsNotSupported.Error(),
		},
		"not_found": {
			url:            "/some/resource",
			status:         http.StatusNotFound,
			expectedErrMsg: ErrNotFound.Error(),
		},
		"invalid_url": {
			url:            "/%",
			expectedErrMsg: "invalid URL escape",
		},
	}

	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)
		})
	}
}