package tests;

import static org.junit.Assert.*;
import httpserver.*;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.HashMap;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class HTTPRequestTest {
  
  private static ServerSocket server;
  
  @Before
  public void setUp() throws IOException {
    HTTPRequest.setHandlerFactory(new MockHTTPHandlerFactory());
    server = new ServerSocket(MockClient.DESIRED_PORT);
  }
  
  @After
  public void tearDown() throws IOException {
    server.close();
  }
  
  @Test
  public void simpleGETRequest() {
    try {
      MockClient c = new MockClient();
      c.fillInSocket();
      HTTPRequest r = new HTTPRequest(server.accept());
      
      
      assertEquals(c.getRequestType(), r.getRequestType());
      assertEquals(c.getPath(), r.getPath());
      assertEquals(c.getGetData(), r.getGetData());
      assertEquals(c.getHeaders(), r.getHeaders());
      assertEquals(c.getPostData(), r.getPostData());
    }
    catch (HTTPException | IOException e) {
      e.printStackTrace();
      fail("Exception occured...");
    }
  }
  
  @Test
  public void simplePOSTRequest() {
    HashMap<String, String> data = new HashMap<String, String>();
    
    data.put("name", "don");
    data.put("a", "b");
    data.put("c", "d");
    
    try {
      MockClient c = new MockClient();
      c.getPostData().putAll(data);
      c.setRequestType("POST");
      
      c.fillInSocket();
      HTTPRequest r = new HTTPRequest(server.accept());
      
      assertEquals(c.getRequestType(), r.getRequestType());
      assertEquals(c.getPostData(), r.getPostData());
      assertEquals(c.getHeaders(), r.getHeaders());
    }
    catch (HTTPException | IOException e) {
      e.printStackTrace();
      fail("Exception occured...");
    }
  }
  

}



@@ -191,22 +191,81 @@
 g.junit.
-Before
+AfterClass;%0Aimport org.junit.Before;%0Aimport org.junit.BeforeClass
 ;%0Aimport
@@ -367,16 +367,21 @@
  @Before
+Class
 %0A  publi
@@ -386,18 +386,24 @@
 lic 
-void setUp
+static void init
 () t
@@ -558,16 +558,21 @@
   @After
+Class
 %0A  publi
@@ -577,21 +577,26 @@
 lic 
-void tearDown
+static void deinit
 () t

test is more efficient now, which is good, right?