(t *testing.T, responses map[string]json.RawMessage)
| 41 | ) |
| 42 | |
| 43 | func CreateTestServer(t *testing.T, responses map[string]json.RawMessage) *httptest.Server { |
| 44 | return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 45 | response := responses[r.FormValue("command")] |
| 46 | rawResult, _ := getRawValue(response) |
| 47 | |
| 48 | var result map[string]json.RawMessage |
| 49 | err := json.Unmarshal(rawResult, &result) |
| 50 | if err != nil { |
| 51 | fmt.Fprintln(w, string(response)) |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | // Since we're using a sync client, pass the job result as the response |
| 56 | val, ok := result["jobresult"] |
| 57 | if !ok { |
| 58 | fmt.Fprintln(w, string(response)) |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | err = json.Unmarshal(val, &result) |
| 63 | if err != nil { |
| 64 | fmt.Fprintln(w, string(response)) |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | // Handle success response separately |
| 69 | if _, ok := result["success"]; ok { |
| 70 | fmt.Fprintf(w, `{"jobresult":{"success":true}}`) |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | fmt.Fprintln(w, string(val)) |
| 75 | |
| 76 | })) |
| 77 | } |
| 78 | |
| 79 | func getRawValue(b json.RawMessage) (json.RawMessage, error) { |
| 80 | var m map[string]json.RawMessage |
no test coverage detected