Skip to content

Commit 3fd4284

Browse files
committed
rm httpbin from rest tests
1 parent d63a859 commit 3fd4284

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

internal/services/rest/rest_test.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package rest_test
22

33
import (
4+
"fmt"
45
"net/http"
6+
"net/http/httptest"
7+
"strconv"
58
"testing"
69
"time"
710

@@ -17,6 +20,37 @@ func TestRest(t *testing.T) {
1720
}
1821

1922
var _ = Describe("Rest", func() {
23+
var testServer *httptest.Server
24+
25+
BeforeEach(func() {
26+
testServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
27+
query := r.URL.Query()
28+
29+
if code := query.Get("code"); code != "" {
30+
if statusCode, err := strconv.Atoi(code); err == nil {
31+
w.WriteHeader(statusCode)
32+
return
33+
}
34+
}
35+
if sleep := query.Get("sleep"); sleep != "" {
36+
if duration, err := time.ParseDuration(sleep); err == nil {
37+
time.Sleep(duration)
38+
}
39+
}
40+
if query.Get("print-headers") == "true" {
41+
w.Header().Set("Content-Type", "application/json")
42+
w.WriteHeader(http.StatusOK)
43+
_, _ = fmt.Fprintf(w, `{"headers": {"%s": "%s"}}`,
44+
"Test-Header", r.Header.Get("Test-Header"))
45+
return
46+
}
47+
48+
w.Header().Set("Content-Type", "application/json")
49+
w.WriteHeader(http.StatusOK)
50+
_, _ = w.Write([]byte(`success`))
51+
}))
52+
})
53+
2054
Context("SendRequest", func() {
2155
It("should return error when invalid URL is provided", func() {
2256
req := &rest.Request{
@@ -30,7 +64,7 @@ var _ = Describe("Rest", func() {
3064

3165
It("should return error when unexpected status code is received", func() {
3266
req := &rest.Request{
33-
URL: "https://httpbin.org/status/500",
67+
URL: testServer.URL + "?code=500",
3468
Method: "GET",
3569
Timeout: 30 * time.Second,
3670
}
@@ -40,18 +74,18 @@ var _ = Describe("Rest", func() {
4074

4175
It("should return the correct body when a valid request is made", func() {
4276
req := &rest.Request{
43-
URL: "https://httpbin.org/get",
77+
URL: testServer.URL,
4478
Method: "GET",
4579
Timeout: 30 * time.Second,
4680
}
4781
resp, err := rest.SendRequest(req, []int{http.StatusOK})
4882
Expect(err).NotTo(HaveOccurred())
49-
Expect(resp.Body).To(ContainSubstring("\"url\": \"https://httpbin.org/get\""))
83+
Expect(resp.Body).To(Equal("success"))
5084
})
5185

5286
It("should timeout when the request takes longer than the specified timeout", func() {
5387
req := &rest.Request{
54-
URL: "https://httpbin.org/delay/3",
88+
URL: testServer.URL + "?sleep=2s",
5589
Method: "GET",
5690
Timeout: 1 * time.Second,
5791
}
@@ -62,7 +96,7 @@ var _ = Describe("Rest", func() {
6296

6397
It("should return the correct headers when a valid request is made", func() {
6498
req := &rest.Request{
65-
URL: "https://httpbin.org/headers",
99+
URL: testServer.URL + "?print-headers=true",
66100
Method: "GET",
67101
Headers: map[string]string{"Test-Header": "Test-Value"},
68102
Timeout: 30 * time.Second,

0 commit comments

Comments
 (0)