@Override
  public WebhookDelivery call(Webhook webhook, WebhookPayload payload) {
    WebhookDelivery.Builder builder = new WebhookDelivery.Builder();
    long startedAt = system.now();
    builder
      .setAt(startedAt)
      .setPayload(payload)
      .setWebhook(webhook);

    try {
      HttpUrl url = HttpUrl.parse(webhook.getUrl());
      if (url == null) {
        throw new IllegalArgumentException("Webhook URL is not valid: " + webhook.getUrl());
      }
      builder.setEffectiveUrl(HttpUrlHelper.obfuscateCredentials(webhook.getUrl(), url));
      Request request = buildHttpRequest(url, webhook, payload);
      try (Response response = execute(request)) {
        builder.setHttpStatus(response.code());
      }
    } catch (Exception e) {
      builder.setError(e);
    }

    return builder
      .setDurationInMs((int) (system.now() - startedAt))
      .build();
  }
@Test
  public void post_payload_to_http_server() throws Exception {
    Webhook webhook = new Webhook(WEBHOOK_UUID, PROJECT_UUID, CE_TASK_UUID, randomAlphanumeric(40),
      "my-webhook", server.url("/ping").toString(), null);

    server.enqueue(new MockResponse().setBody("pong").setResponseCode(201));
    WebhookDelivery delivery = newSender(false).call(webhook, PAYLOAD);

    assertThat(delivery.getHttpStatus()).hasValue(201);
    assertThat(delivery.getWebhook().getUuid()).isEqualTo(WEBHOOK_UUID);
    assertThat(delivery.getDurationInMs().get()).isNotNegative();
    assertThat(delivery.getError()).isEmpty();
    assertThat(delivery.getAt()).isEqualTo(NOW);
    assertThat(delivery.getWebhook()).isSameAs(webhook);
    assertThat(delivery.getPayload()).isSameAs(PAYLOAD);

    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getMethod()).isEqualTo("POST");
    assertThat(recordedRequest.getPath()).isEqualTo("/ping");
    assertThat(recordedRequest.getBody().readUtf8()).isEqualTo(PAYLOAD.getJson());
    assertThat(recordedRequest.getHeader("User-Agent")).isEqualTo("SonarQube/6.2");
    assertThat(recordedRequest.getHeader("Content-Type")).isEqualTo("application/json; charset=utf-8");
    assertThat(recordedRequest.getHeader("X-SonarQube-Project")).isEqualTo(PAYLOAD.getProjectKey());
    assertThat(recordedRequest.getHeader("X-Sonar-Webhook-HMAC-SHA256")).isNull();
  }