package integration.proxy;

import java.util.ArrayList;
import java.util.List;

import com.codeborne.selenide.proxy.SelenideProxyServer;
import integration.IntegrationTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.WebDriverRunner.closeWebDriver;
import static com.codeborne.selenide.WebDriverRunner.getSelenideProxy;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

public class ProxyServerUsageTest extends IntegrationTest {
  List<String> requests = new ArrayList<>();
  List<String> responses = new ArrayList<>();

  @Before
  @After
  public void setUp() {
    closeWebDriver();
  }

  @Test
  public void canAddInterceptorsToProxyServer() {
    openFile("file_upload_form.html");

    SelenideProxyServer selenideProxy = getSelenideProxy();

    selenideProxy.addRequestFilter("proxy-usages.request", (request, contents, messageInfo) -> {
      String url = messageInfo.getUrl();
      if (!url.contains("gstatic.com") || !url.contains("google.com")) {
        requests.add(url + "\n\n" + contents.getTextContents());
      }
      return null;
    });
    selenideProxy.addResponseFilter("proxy-usages.response", (response, contents, messageInfo) -> {
      String url = messageInfo.getUrl();
      if (!url.contains("gstatic.com")|| !url.contains("google.com")) {
        responses.add(url + "\n\n" + contents.getTextContents());
      }
    });

    $("#cv").uploadFromClasspath("hello_world.txt");
    $("#submit").click();

    assertNotNull("Check browser mob proxy instance", getSelenideProxy().getProxy());

    assertEquals("All requests: " + requests, 1, requests.size());
    assertEquals("All responses: " + responses, 1, responses.size());

    assertThat(requests.get(0), containsString("/upload"));
    assertThat(requests.get(0), containsString("Content-Disposition: form-data; name=\"cv\"; filename=\"hello_world.txt\""));
    assertThat(requests.get(0), containsString("Hello, WinRar!"));

    assertThat(responses.get(0), containsString("<h3>Uploaded 1 files</h3>"));
  }
}

@@ -1149,65 +1149,39 @@
 f (!
-url.contains(%22gstatic.com%22) %7C%7C !url.contains(%22google.com%22
+isChromeOwnTechnicalRequest(url
 )) %7B
@@ -1437,64 +1437,39 @@
 f (!
-url.contains(%22gstatic.com%22)%7C%7C !url.contains(%22google.com%22
+isChromeOwnTechnicalRequest(url
 )) %7B
@@ -2193,11 +2193,146 @@
 %22));%0A  %7D
+%0A%0A  private boolean isChromeOwnTechnicalRequest(String url) %7B%0A    return url.contains(%22gstatic.com%22) %7C%7C url.contains(%22google.com%22);%0A  %7D
 %0A%7D%0A

fix bug in test: `!url.contains("gstatic.com") || !url.contains("google.com")` was always `true`