-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathpkgregister_test.go
71 lines (65 loc) · 1.78 KB
/
pkgregister_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
package pkgregister
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func packageRegHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
// Package registration response
d := pkgRegisterResult{}
err := r.ParseMultipartForm(5000)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
mForm := r.MultipartForm
// Get file data
f := mForm.File["filedata"][0]
// Construct an artificial package ID to return
d.ID = fmt.Sprintf("%s-%s", mForm.Value["name"][0], mForm.Value["version"][0])
d.Filename = f.Filename
d.Size = f.Size
// Marshal outgoing package registration response
jsonData, err := json.Marshal(d)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(jsonData))
} else {
http.Error(w, "Invalid HTTP method specified", http.StatusMethodNotAllowed)
return
}
}
func startTestPackageServer() *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(packageRegHandler))
return ts
}
func TestRegisterPackageData(t *testing.T) {
ts := startTestPackageServer()
defer ts.Close()
p := pkgData{
Name: "mypackage",
Version: "0.1",
Filename: "mypackage-0.1.tar.gz",
Bytes: strings.NewReader("data"),
}
pResult, err := registerPackageData(ts.URL, p)
if err != nil {
t.Fatal(err)
}
if pResult.ID != fmt.Sprintf("%s-%s", p.Name, p.Version) {
t.Errorf("Expected package ID to be %s-%s, Got: %s", p.Name, p.Version, pResult.ID)
}
if pResult.Filename != p.Filename {
t.Errorf("Expected package filename to be %s, Got: %s", p.Filename, pResult.Filename)
}
if pResult.Size != 4 {
t.Errorf("Expected package size to be 4, Got: %d", pResult.Size)
}
}