(wr *WebhookRequest)
| 381 | } |
| 382 | |
| 383 | func GetRouterWithHandler(wr *WebhookRequest) http.Handler { |
| 384 | r := http.NewServeMux() |
| 385 | r.HandleFunc("/slow", func(w http.ResponseWriter, r *http.Request) { |
| 386 | time.Sleep(1 * time.Second) |
| 387 | wr.IncrementCount() |
| 388 | _, _ = fmt.Fprintf(w, "OK") |
| 389 | }) |
| 390 | r.HandleFunc("/slow_2s", func(w http.ResponseWriter, r *http.Request) { |
| 391 | time.Sleep(2 * time.Second) |
| 392 | wr.IncrementCount() |
| 393 | _, _ = fmt.Fprintf(w, "OK") |
| 394 | }) |
| 395 | r.HandleFunc("/slow_5s", func(w http.ResponseWriter, r *http.Request) { |
| 396 | time.Sleep(5 * time.Second) |
| 397 | wr.IncrementCount() |
| 398 | _, _ = fmt.Fprintf(w, "OK") |
| 399 | }) |
| 400 | r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 401 | _ = r.ParseForm() |
| 402 | body, err := io.ReadAll(r.Body) |
| 403 | if err != nil { |
| 404 | log.Printf("Error trying to read body: %s", err) |
| 405 | } |
| 406 | if len(body) > 0 { |
| 407 | wr.AddPayload(body) |
| 408 | var payload map[string]interface{} |
| 409 | err = base.JSONUnmarshal(body, &payload) |
| 410 | if err != nil { |
| 411 | log.Printf("Error trying parses the JSON-encoded data: %s", err) |
| 412 | } |
| 413 | floatValue, ok := payload["value"].(float64) |
| 414 | if ok { |
| 415 | wr.IncrementSum(floatValue) |
| 416 | } |
| 417 | } |
| 418 | if len(r.Form) > 0 { |
| 419 | log.Printf("Handled request with form: %v", r.Form) |
| 420 | floatValue, err := strconv.ParseFloat(r.Form.Get("value"), 64) |
| 421 | if err == nil { |
| 422 | wr.IncrementSum(floatValue) |
| 423 | } |
| 424 | } |
| 425 | wr.IncrementCount() |
| 426 | _, _ = fmt.Fprintf(w, "OK") |
| 427 | }) |
| 428 | return r |
| 429 | } |
| 430 | |
| 431 | func InitWebhookTest() (*httptest.Server, *WebhookRequest) { |
| 432 | wr := &WebhookRequest{} |
no test coverage detected