forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_test.go
134 lines (108 loc) · 3.41 KB
/
transport_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cargo_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/packit/v2/cargo"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testTransport(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect
context("Drop", func() {
var transport cargo.Transport
it.Before(func() {
transport = cargo.NewTransport()
})
context("when the given uri is online", func() {
var server *httptest.Server
it.Before(func() {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/some-bundle":
fmt.Fprint(w, "some-bundle-contents")
default:
http.NotFound(w, req)
}
}))
})
it.After(func() {
server.Close()
})
it("downloads the file from a URI", func() {
bundle, err := transport.Drop("", fmt.Sprintf("%s/some-bundle", server.URL))
Expect(err).NotTo(HaveOccurred())
contents, err := io.ReadAll(bundle)
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal("some-bundle-contents"))
Expect(bundle.Close()).To(Succeed())
})
context("failure cases", func() {
context("when the uri is malformed", func() {
it("returns an error", func() {
_, err := transport.Drop("", "%%%%")
Expect(err).To(MatchError(ContainSubstring("failed to parse request uri")))
Expect(err).To(MatchError(ContainSubstring("invalid URL escape")))
})
})
context("when the request fails", func() {
it.Before(func() {
server.Close()
})
it("returns an error", func() {
_, err := transport.Drop("", fmt.Sprintf("%s/some-bundle", server.URL))
Expect(err).To(MatchError(ContainSubstring("failed to make request")))
Expect(err).To(MatchError(ContainSubstring("connection refused")))
})
})
context("when the http status indicates an error", func() {
it("returns an error", func() {
_, err := transport.Drop("", fmt.Sprintf("%s/some-bundle-that-does-not-exist", server.URL))
Expect(err).To(MatchError(ContainSubstring("unexpected status code 404 while fetching")))
})
})
})
})
context("when the uri is for a file", func() {
var (
path string
dir string
)
it.Before(func() {
var err error
dir, err = os.MkdirTemp("", "bundle")
Expect(err).NotTo(HaveOccurred())
path = "some-file"
err = os.WriteFile(filepath.Join(dir, path), []byte("some-bundle-contents"), 0644)
Expect(err).NotTo(HaveOccurred())
})
it.After(func() {
Expect(os.RemoveAll(dir)).To(Succeed())
})
it("returns the file descriptor", func() {
bundle, err := transport.Drop(dir, fmt.Sprintf("file://%s", path))
Expect(err).NotTo(HaveOccurred())
contents, err := io.ReadAll(bundle)
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal("some-bundle-contents"))
Expect(bundle.Close()).To(Succeed())
})
context("failure cases", func() {
it.Before(func() {
Expect(os.RemoveAll(dir)).To(Succeed())
})
context("when the file does not exist", func() {
it("returns an error", func() {
_, err := transport.Drop(dir, fmt.Sprintf("file://%s", path))
Expect(err).To(MatchError(ContainSubstring("failed to open file")))
Expect(err).To(MatchError(ContainSubstring("no such file or directory")))
})
})
})
})
})
}