This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
85 lines (72 loc) · 2.05 KB
/
main_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
package main_test
import (
"fmt"
"net/http"
"os"
"os/exec"
"strconv"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
)
var _ = Describe("Routing", func() {
var url string
var session *gexec.Session
BeforeEach(func() {
os.Setenv("PORT", strconv.Itoa(appPort))
os.Setenv("USERNAME", username)
os.Setenv("PASSWORD", password)
command := exec.Command(pathToExecutable)
var err error
session, err = gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Out).Should(gbytes.Say("Prodda started"))
url = fmt.Sprintf("http://localhost:%d", appPort)
})
AfterEach(func() {
session.Terminate().Wait()
})
Describe("/", func() {
It("returns 200 without authentication", func() {
resp, err := http.Get(url)
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))
})
})
Describe("/api/v0/tasks", func() {
BeforeEach(func() {
url = fmt.Sprintf("%s/api/v0/tasks/", url)
})
It("returns 401 when no credentials provided", func() {
resp, err := http.Get(url)
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusUnauthorized))
})
It("returns 401 when bad credentials provided", func() {
req, err := http.NewRequest("GET", url, nil)
Expect(err).NotTo(HaveOccurred())
req.SetBasicAuth("baduser", "somepassword")
client := &http.Client{}
resp, err := client.Do(req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusUnauthorized))
})
Context("when authenticated and authorized", func() {
var req *http.Request
BeforeEach(func() {
var err error
req, err = http.NewRequest("", url, nil)
Expect(err).NotTo(HaveOccurred())
req.SetBasicAuth(username, password)
})
It("returns 200 for GET", func() {
req.Method = "GET"
client := &http.Client{}
resp, err := client.Do(req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))
})
})
})
})