-
Notifications
You must be signed in to change notification settings - Fork 14
/
labels_test.go
57 lines (44 loc) · 1.19 KB
/
labels_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
package bamboo_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
bamboo "github.com/rcarmstrong/go-bamboo"
)
var (
testLabel = "prod"
resultLabelKey = "TEST-TEST-1"
)
func TestAddLabel(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(addLabelStub))
defer ts.Close()
client := bamboo.NewSimpleClient(nil, "", "")
client.SetURL(ts.URL)
label := &bamboo.Label{
Name: testLabel,
ResultKey: resultLabelKey,
}
success, resp, err := client.Labels.AddLabel(label)
if err != nil {
t.Error(err)
}
if success == false || resp.StatusCode != 204 {
t.Error(fmt.Sprintf("Adding Label \"%s\" was unsuccessful. Returned %s", testLabel, resp.Status))
}
}
func addLabelStub(w http.ResponseWriter, r *http.Request) {
label := &bamboo.Label{}
expectedURI := fmt.Sprintf("/rest/api/latest/result/%s/label.json", resultLabelKey)
json.NewDecoder(r.Body).Decode(label)
if label.Name != testLabel {
http.Error(w, "Labels do not match", http.StatusBadRequest)
return
}
if r.RequestURI != expectedURI {
http.Error(w, fmt.Sprintf("URI did not match expected %s", resultLabelKey), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNoContent)
}