-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathpackage_get_handler_test.go
119 lines (103 loc) · 2.28 KB
/
package_get_handler_test.go
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
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"context"
"errors"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
_ "gocloud.dev/blob/fileblob"
)
func TestPackageGetHandlerNoData(t *testing.T) {
packageBucket, err := getTestBucket(t.TempDir())
if err != nil {
t.Fatal(err)
}
defer packageBucket.Close()
config := appConfig{
logger: log.New(
os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile,
),
packageBucket: packageBucket,
db: testDb,
}
mux := http.NewServeMux()
setupHandlers(mux, config)
ts := httptest.NewServer(mux)
defer ts.Close()
client := http.Client{}
resp, err := client.Get(ts.URL + "/api/packages?owner_id=1&name=test-package&version=0.1")
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusNotFound {
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
t.Log(string(data))
t.Fatalf(
"Expected a HTTP 404 response, Got: %v\n",
resp.StatusCode,
)
}
}
func TestPackageGetHandler(t *testing.T) {
packageBucket, err := getTestBucket(t.TempDir())
if err != nil {
t.Fatal(err)
}
defer packageBucket.Close()
testObjectId := "pkg-0.1-pkg-0.1.tar.gz"
// create a test object
err = packageBucket.WriteAll(
context.Background(),
testObjectId, []byte("test-data"),
nil,
)
if err != nil {
t.Fatal(err)
}
config := appConfig{
logger: log.New(
os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile,
),
packageBucket: packageBucket,
db: testDb,
}
// udpate package metadata for the test object
err = updateDb(
config,
pkgRow{
OwnerId: 1,
Name: "pkg",
Version: "0.1",
ObjectStoreId: testObjectId,
},
)
if err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
setupHandlers(mux, config)
ts := httptest.NewServer(mux)
defer ts.Close()
var redirectUrl string
client := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
redirectUrl = req.URL.String()
return errors.New("no redirect")
},
}
_, err = client.Get(ts.URL + "/api/packages?owner_id=1&name=pkg&version=0.1")
if err == nil {
t.Fatal("Expected error: no redirect, Got nil")
}
if !strings.HasPrefix(redirectUrl, "file:///") {
t.Fatalf("Expected redirect url to start with file:///, got: %v", redirectUrl)
}
}