This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
client_test.go
161 lines (128 loc) · 4.19 KB
/
client_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package client_test
import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/remove-bg/go/client"
"gopkg.in/h2non/gock.v1"
"net/http"
"path"
"runtime"
)
var _ = Describe("Client", func() {
var (
fixtureFile string
bgFixtureFile string
subject client.Client
)
BeforeEach(func() {
_, testFile, _, _ := runtime.Caller(0)
fixtureFile = path.Join(path.Dir(testFile), "../fixtures/person-in-field.jpg")
bgFixtureFile = path.Join(path.Dir(testFile), "../fixtures/background.jpg")
subject = client.Client{
HTTPClient: http.Client{},
}
})
AfterEach(func() {
gock.Off()
})
It("requests the background removal", func() {
gock.New("https://api.remove.bg").
Post("/v1.0/removebg").
MatchHeader("X-Api-Key", "^api-key$").
Reply(200).
SetHeader("Content-Type", "image/png").
BodyString("data")
result, contentType, err := subject.RemoveFromFile(fixtureFile, "api-key", map[string]string{})
Expect(err).To(Not(HaveOccurred()))
Expect(result).To(Equal([]byte("data")))
Expect(contentType).To(Equal("image/png"))
Expect(gock.IsDone()).To(BeTrue())
})
It("attaches the image file", func() {
matcher := newMultipartAttachmentMatcher("image_file", "person-in-field.jpg")
gock.New("https://api.remove.bg").
Post("/v1.0/removebg").
SetMatcher(matcher).
Reply(200).
BodyString("data")
_, _, err := subject.RemoveFromFile(fixtureFile, "api-key", map[string]string{})
Expect(err).To(Not(HaveOccurred()))
Expect(gock.IsDone()).To(BeTrue())
})
It("attaches a background image file if specified", func() {
imageMatcher := newMultipartAttachmentMatcher("image_file", "person-in-field.jpg")
bgImagematcher := newMultipartAttachmentMatcher("bg_image_file", "background.jpg")
gock.New("https://api.remove.bg").
Post("/v1.0/removebg").
SetMatcher(imageMatcher).
SetMatcher(bgImagematcher).
Reply(200).
BodyString("data")
params := map[string]string{
"bg_image_file": bgFixtureFile,
}
_, _, err := subject.RemoveFromFile(fixtureFile, "api-key", params)
Expect(err).To(Not(HaveOccurred()))
Expect(gock.IsDone()).To(BeTrue())
})
It("includes the client version", func() {
gock.New("https://api.remove.bg").
Post("/v1.0/removebg").
MatchHeader("User-Agent", `^remove-bg-go-\d+\.\d+\.\d+$`).
Reply(200).
BodyString("data")
subject.RemoveFromFile(fixtureFile, "api-key", map[string]string{})
Expect(gock.IsDone()).To(BeTrue())
})
Context("server HTTP error", func() {
It("returns a clear error", func() {
gock.New("https://api.remove.bg").
Post("/v1.0/removebg").
Reply(500)
result, _, err := subject.RemoveFromFile(fixtureFile, "api-key", map[string]string{})
Expect(result).To(BeNil())
Expect(err).To(MatchError("Unable to process image http_status=500"))
})
})
Context("client HTTP error", func() {
It("parses the JSON error messages", func() {
jsonError := `{"errors": [{"title": "File too large"}, {"title": "Second error"}]}`
gock.New("https://api.remove.bg").
Post("/v1.0/removebg").
Reply(400).
BodyString(jsonError)
result, _, err := subject.RemoveFromFile(fixtureFile, "api-key", map[string]string{})
Expect(result).To(BeNil())
Expect(err).To(MatchError("File too large, Second error"))
})
})
Context("input file doesn't exist", func() {
It("returns a clear error", func() {
nonExistentFile := "/tmp/not-a-file"
result, _, err := subject.RemoveFromFile(nonExistentFile, "api-key", map[string]string{})
Expect(result).To(BeNil())
Expect(err).To(MatchError("Unable to read file"))
})
})
})
func newMultipartAttachmentMatcher(key string, expectedFilename string) *gock.MockMatcher {
// Create a new custom matcher with HTTP headers only matchers
matcher := gock.NewBasicMatcher()
// Add a custom match function
matcher.Add(func(req *http.Request, ereq *gock.Request) (bool, error) {
_, header, err := req.FormFile(key)
if err != nil {
return false, err
}
if header.Size == 0 {
return false, fmt.Errorf("Attachment is empty: %v", header.Size)
}
if header.Filename == expectedFilename {
return true, nil
} else {
return false, fmt.Errorf("Image filename was: %s", header.Filename)
}
})
return matcher
}