MCPcopy
hub / github.com/crowdsecurity/crowdsec / initBasicMuxMock

Function initBasicMuxMock

pkg/apiclient/auth_service_test.go:33–66  ·  view source on GitHub ↗
(t *testing.T, mux *http.ServeMux, path string)

Source from the content-addressed store, hash-verified

31}
32
33func initBasicMuxMock(t *testing.T, mux *http.ServeMux, path string) {
34 loginsForMockErrorCases := getLoginsForMockErrorCases()
35
36 mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
37 testMethod(t, r, "POST")
38
39 buf := new(bytes.Buffer)
40 _, _ = buf.ReadFrom(r.Body)
41 newStr := buf.String()
42
43 var payload BasicMockPayload
44
45 err := json.Unmarshal([]byte(newStr), &payload)
46 if err != nil || payload.MachineID == "" || payload.Password == "" {
47 log.Info("Bad payload")
48 w.WriteHeader(http.StatusBadRequest)
49 }
50
51 var responseBody string
52
53 responseCode, hasFoundErrorMock := loginsForMockErrorCases[payload.MachineID]
54 if !hasFoundErrorMock {
55 responseCode = http.StatusOK
56 responseBody = `{"code":200,"expire":"2029-11-30T14:14:24+01:00","token":"toto"}`
57 } else {
58 responseBody = fmt.Sprintf("Error %d", responseCode)
59 }
60
61 log.Printf("MockServerReceived > %s // Login : [%s] => Mux response [%d]", newStr, payload.MachineID, responseCode)
62
63 w.WriteHeader(responseCode)
64 fmt.Fprintf(w, `%s`, responseBody)
65 })
66}
67
68/**
69 * Test the RegisterClient function

Callers 2

TestWatcherRegisterFunction · 0.85
TestWatcherAuthFunction · 0.85

Calls 4

testMethodFunction · 0.85
StringMethod · 0.45
InfoMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…