/*
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.cloud.tools.eclipse.usagetracker;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.cloud.tools.eclipse.test.util.http.TestHttpServer;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;

public class HttpUtilWithServerTest {

  @Rule public TestHttpServer server = new TestHttpServer("", "You're good!");

  private static final String LONG_PARAMETER = "Exception in thread \"main\""
      + " java.lang.IllegalArgumentException: Wrong argument!\n\tat B.main(B.java:73)";

  @Test
  public void testSendPostMultipart() throws IOException {
    Map<String, String> parameters = new HashMap<>();
    parameters.put("dp", "/virtual/some-event-type/some-event-name");
    parameters.put("exception_info", LONG_PARAMETER);
    parameters.put("product", "CT4E");

    assertEquals(200, HttpUtil.sendPostMultipart(server.getAddress(), parameters));
    assertEquals("POST", server.getRequestMethod());
    assertTrue(server.getRequestHeaders().get("User-Agent")
        .startsWith("gcloud-eclipse-tools/"));
    assertTrue(server.getRequestHeaders().get("Content-Type")
        .startsWith("multipart/form-data; boundary="));

    Map<String, String[]> parsedParameters = server.getRequestParameters();
    assertEquals("/virtual/some-event-type/some-event-name", parsedParameters.get("dp")[0]);
    assertEquals(LONG_PARAMETER, parsedParameters.get("exception_info")[0]);
    assertEquals("CT4E", parsedParameters.get("product")[0]);
  }
}

@@ -1216,98 +1216,85 @@
 %0A%0A  
-@Test%0A  public void 
+priva
 te
+ 
 st
-SendPostMultipart() throws IOException %7B%0A    Map%3CString, String%3E p
+atic final Map%3CString, String%3E testParameters;%0A  static %7B%0A    testP
 aram
@@ -1318,25 +1318,29 @@
 ap%3C%3E();%0A    
-p
+testP
 arameters.pu
@@ -1392,25 +1392,29 @@
 name%22);%0A    
-p
+testP
 arameters.pu
@@ -1454,17 +1454,21 @@
 R);%0A    
-p
+testP
 arameter
@@ -1493,16 +1493,88 @@
 CT4E%22);%0A
+  %7D;%0A%0A  @Test%0A  public void testSendPostMultipart() throws IOException %7B
 %0A    ass
@@ -1636,17 +1636,21 @@
 ress(), 
-p
+testP
 arameter
@@ -1668,154 +1668,512 @@
 sert
-Equals(%22POST%22, server.getRequestMethod());%0A    assertTrue(server.getRequestHeaders().get(%22User-Agent%22)%0A        .startsWith(%22gcloud-eclipse-tools/%22
+True(server.getRequestHeaders().get(%22Content-Type%22)%0A        .startsWith(%22multipart/form-data; boundary=%22));%0A%0A    verifyPostRequest();%0A  %7D%0A%0A  @Test%0A  public void testSendPost() throws IOException %7B%0A    assertEquals(200, HttpUtil.sendPost(server.getAddress(), testParameters));%0A    assertEquals(%22application/x-www-form-urlencoded%22,%0A        server.getRequestHeaders().get(%22Content-Type%22));%0A%0A    verifyPostRequest();%0A  %7D%0A%0A  private void verifyPostRequest() %7B%0A    assertEquals(%22POST%22, server.getRequestMethod(
 ));%0A
@@ -2215,36 +2215,34 @@
 ders().get(%22
-Content-Type
+User-Agent
 %22)%0A        .
@@ -2257,38 +2257,29 @@
 th(%22
-multipart/form-data; boundary=
+gcloud-eclipse-tools/
 %22));

Add HttpUtil test (#1535)