package com.zipcoder.server;

import com.zipcoder.server.mocks.MockSocket;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class RequestHandlerTest {
    private MockSocket socket;
    private RequestHandler handler;

    @Before
    public void setUp(){
        socket = new MockSocket();
        handler = new RequestHandler(socket);
    }

    @Test
    public void testRun() {
        String expectedResponse = "HTTP/1.1 200 OK\n"
                                    + "text/html\n"
                                    + "Content-Length: 13\n"
                                    + "\r\n"
                                    + "Hello, world!";


        socket.setRequest("GET / HTTP/1.1");

        handler.run();
        Assert.assertEquals(expectedResponse, socket.getResponse());
    }

    @Test
    public void testRun_whenExceptionIsThrownSocketIsClosed() {
        socket.setRequest("GET /nonexistence HTTP/1.1");

        handler.run();
        Assert.assertTrue(socket.isClosed());
    }
}
