-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
enqueue_test.go
79 lines (67 loc) · 2.11 KB
/
enqueue_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
package main_test
import (
"fmt"
"net/http/httptest"
"os"
"testing"
"github.com/go-martini/martini"
. "github.com/rafecolton/docker-builder"
"github.com/rafecolton/docker-builder/server"
"github.com/rafecolton/docker-builder/server/webhook"
"github.com/rafecolton/go-gitutils"
)
func init() {
webhook.TestMode(true)
}
func testServer() (testServer *martini.ClassicMartini) {
r := martini.NewRouter()
m := martini.New()
m.Use(martini.Recovery())
m.Use(martini.Static("public"))
m.MapTo(r, (*martini.Routes)(nil))
m.Action(r.Handle)
testServer = &martini.ClassicMartini{m, r}
testServer.Post(server.BuildRoute, webhook.DockerBuild)
return
}
var enqueuerHost = "http://foo:bar@docker-build-server.example.com:5000"
func testEnqueuer() *Enqueuer {
return NewEnqueuer(EnqueueOptions{
Bobfile: "Bobfile.foo",
Host: enqueuerHost,
Top: os.Getenv("PWD"),
})
}
func TestEnqueueRequest(t *testing.T) {
var testServer = testServer()
var recorder = httptest.NewRecorder()
var enqueuer = testEnqueuer()
req, err := enqueuer.Request()
if err != nil {
t.Errorf("error making request: %q", err.Error())
}
testServer.ServeHTTP(recorder, req)
if recorder.Code != webhook.AsyncSuccessCode {
t.Errorf("expected response code %d, got %d", webhook.AsyncSuccessCode, recorder.Code)
}
if recorder.Body.String() != webhook.AsyncSuccessMessage {
t.Errorf("expected response message %q, got %q", webhook.AsyncSuccessMessage, recorder.Body.String())
}
}
func TestEnqueueRequestBody(t *testing.T) {
var enqueuer = testEnqueuer()
var expectedBody = fmt.Sprintf(`{"account":"rafecolton","bobfile":"Bobfile.foo","ref":"%s","repo":"docker-builder"}`, git.Branch(os.Getenv("PWD")))
bodyBytes, err := enqueuer.BodyBytes()
if err != nil {
t.Error(err.Error())
}
if string(bodyBytes) != expectedBody {
t.Errorf("expected request body %s, got %s", expectedBody, string(bodyBytes))
}
}
func TestEnqueueRequestPath(t *testing.T) {
var enqueuer = testEnqueuer()
if enqueuer.RequestPath() != enqueuerHost+"/docker-build" {
t.Errorf("expected request host %q, got %q", enqueuerHost+"/docker-build", enqueuer.RequestPath())
}
}