Skip to content

Commit

Permalink
Add tests for fugacious sender.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarp committed Nov 2, 2016
1 parent efe9134 commit 9a1d673
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 22 deletions.
16 changes: 6 additions & 10 deletions broker.go
Expand Up @@ -33,10 +33,11 @@ type Email struct {
}

type DeployerAccountBroker struct {
uaaClient *http.Client
cfClient *http.Client
logger lager.Logger
config Config
uaaClient *http.Client
cfClient *http.Client
credentialSender CredentialSender
logger lager.Logger
config Config
}

func encodeBody(obj interface{}) (io.Reader, error) {
Expand Down Expand Up @@ -93,12 +94,7 @@ func (b *DeployerAccountBroker) Provision(
return brokerapi.ProvisionedServiceSpec{}, err
}

link, err := CreateEphemeralLink(
b.config.FugaciousAddress,
fmt.Sprintf("%s | %s", instanceID, password),
b.config.FugaciousHours,
b.config.FugaciousMaxViews,
)
link, err := b.credentialSender.Send(fmt.Sprintf("%s | %s", instanceID, password))
if err != nil {
return brokerapi.ProvisionedServiceSpec{}, err
}
Expand Down
29 changes: 18 additions & 11 deletions fugacious.go
@@ -1,24 +1,31 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)

func CreateEphemeralLink(endpoint, body string, hours, maxViews int) (string, error) {
type CredentialSender interface {
Send(message string) (string, error)
}

type FugaciousCredentialSender struct {
endpoint string
hours int
maxViews int
}

func (f FugaciousCredentialSender) Send(message string) (string, error) {
data := map[string]interface{}{
"message": map[string]interface{}{
"body": body,
"hours": hours,
"max_views": maxViews,
"body": message,
"hours": f.hours,
"max_views": f.maxViews,
},
}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(data)

req, _ := http.NewRequest("POST", fmt.Sprintf("%s/m", endpoint), b)
body, _ := encodeBody(data)
req, _ := http.NewRequest("POST", fmt.Sprintf("%s/m", f.endpoint), body)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")

Expand All @@ -32,8 +39,8 @@ func CreateEphemeralLink(endpoint, body string, hours, maxViews int) (string, er
if err != nil {
return "", err
}
if resp.StatusCode != 302 {
return "", fmt.Errorf("Expected status 302; got %d", resp.StatusCode)
if resp.StatusCode != http.StatusFound {
return "", fmt.Errorf("Expected status %d; got %d", http.StatusFound, resp.StatusCode)
}
return resp.Header.Get("Location"), nil
}
53 changes: 53 additions & 0 deletions fugacious_test.go
@@ -0,0 +1,53 @@
package main

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"net/http"
"net/http/httptest"
)

var _ = Describe("Fugacious", func() {
var (
header int
server *httptest.Server
sender FugaciousCredentialSender
)

BeforeEach(func() {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", "https://fugacio.us/m/42")
w.WriteHeader(header)
}))
sender = FugaciousCredentialSender{
endpoint: server.URL,
hours: 24,
maxViews: 4,
}
})

Context("when the status code is expected", func() {
BeforeEach(func() {
header = http.StatusFound
})

It("retrieves a link from the fugacious server", func() {
url, err := sender.Send("testing")
Expect(err).NotTo(HaveOccurred())
Expect(url).To(Equal("https://fugacio.us/m/42"))
})
})

Context("when the status code is unexpected", func() {
BeforeEach(func() {
header = http.StatusNotFound
})

It("complains about the response status", func() {
_, err := sender.Send("testing")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Expected status"))
})
})
})
7 changes: 6 additions & 1 deletion main.go
Expand Up @@ -75,7 +75,12 @@ func main() {
logger: logger,
uaaClient: NewUAAClient(config),
cfClient: NewCFClient(config),
config: config,
credentialSender: FugaciousCredentialSender{
endpoint: config.FugaciousAddress,
hours: config.FugaciousHours,
maxViews: config.FugaciousMaxViews,
},
config: config,
}
credentials := brokerapi.BrokerCredentials{
Username: config.BrokerUsername,
Expand Down
12 changes: 12 additions & 0 deletions main_suite_test.go
@@ -0,0 +1,12 @@
package main

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)

func TestMain(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Main Suite")
}

0 comments on commit 9a1d673

Please sign in to comment.