(t *testing.T, mux *http.ServeMux, path string)
| 31 | } |
| 32 | |
| 33 | func 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 |
no test coverage detected
searching dependent graphs…