-
Notifications
You must be signed in to change notification settings - Fork 4
/
testutils_lagoon.go
55 lines (49 loc) · 1.63 KB
/
testutils_lagoon.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
package internal
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
)
var MockLagoonNumCalls int
var MockLagoonRequestBodies []string
func MockLagoonServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
MockLagoonNumCalls = MockLagoonNumCalls + 1
reqBody, _ := io.ReadAll(r.Body)
MockLagoonRequestBodies = append(MockLagoonRequestBodies, string(reqBody))
// Response for the first request, environment id.
if strings.Contains(string(reqBody), "environmentByKubernetesNamespaceName") {
fmt.Fprintf(w, "{\"data\":{\"environmentByKubernetesNamespaceName\":{\"id\": 50}}}")
} else if strings.Contains(string(reqBody), "deleteProblemsFromSource") { // Response for the deletion.
fmt.Fprintf(w, "{\"data\":{\"deleteProblemsFromSource\":\"success\"}}")
} else if strings.Contains(string(reqBody), "AddFactsByNameInput") { // Response for the add.
fmt.Fprintf(w, "{}")
} else {
panic(string(reqBody))
}
}))
}
func MockLagoonReset() {
MockLagoonNumCalls = 0
MockLagoonRequestBodies = []string{}
}
type MockInsightsRemoteTestState struct {
LastCallBody string
LastCallHeaders map[string]string
LastCallEndpoint string
LastCallStatus string
}
func MockRemoteInsightsServer(state *MockInsightsRemoteTestState) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
state.LastCallEndpoint = r.RequestURI
requestBody, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
}
state.LastCallBody = string(requestBody)
fmt.Fprintf(w, "okay")
}))
}