package org.wisdom.framework.vertx;

import org.junit.Test;
import org.vertx.java.core.http.CaseInsensitiveMultiMap;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.http.HttpVersion;
import org.wisdom.api.http.HeaderNames;
import org.wisdom.api.http.Result;
import org.wisdom.api.http.Status;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class HttpUtilsTest {

    @Test
    public void testIsKeepAlive() throws Exception {
        CaseInsensitiveMultiMap headers = new CaseInsensitiveMultiMap();
        HttpServerRequest req = mock(HttpServerRequest.class);
        when(req.headers()).thenReturn(headers);

        // Connection header set.
        headers.add(HeaderNames.CONNECTION, HttpUtils.CLOSE);
        assertThat(HttpUtils.isKeepAlive(req)).isFalse();

        headers.add(HeaderNames.CONNECTION, HttpUtils.KEEP_ALIVE);
        assertThat(HttpUtils.isKeepAlive(req)).isTrue();

        // Unset connection header
        headers.clear();
        when(req.version()).thenReturn(HttpVersion.HTTP_1_1);
        assertThat(HttpUtils.isKeepAlive(req)).isTrue();

        when(req.version()).thenReturn(HttpVersion.HTTP_1_0);
        assertThat(HttpUtils.isKeepAlive(req)).isFalse();
    }

    @Test
    public void testGetStatusFromResult() throws Exception {
        assertThat(HttpUtils.getStatusFromResult(new Result(Status.OK), false)).isEqualTo(Status.BAD_REQUEST);
        assertThat(HttpUtils.getStatusFromResult(new Result(Status.OK), true)).isEqualTo(Status.OK);
    }

    @Test
    public void testGetContentTypeFromContentTypeAndCharacterSetting() throws Exception {
        assertThat(HttpUtils.getContentTypeFromContentTypeAndCharacterSetting("application/json; charset=\"utf-8\""))
                .isEqualTo("application/json");
        assertThat(HttpUtils.getContentTypeFromContentTypeAndCharacterSetting("application/json"))
                .isEqualTo("application/json");
    }

    @Test
    public void testIsPostOrPut() throws Exception {
        HttpServerRequest req = mock(HttpServerRequest.class);
        when(req.method()).thenReturn("post");
        assertThat(HttpUtils.isPostOrPut(req)).isTrue();
        when(req.method()).thenReturn("put");
        assertThat(HttpUtils.isPostOrPut(req)).isTrue();
        when(req.method()).thenReturn("get");
        assertThat(HttpUtils.isPostOrPut(req)).isFalse();
    }
}
@@ -1,8 +1,665 @@
+/*%0A * #%25L%0A * Wisdom-Framework%0A * %25%25%0A * Copyright (C) 2013 - 2014 Wisdom Framework%0A * %25%25%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A * %0A *      http://www.apache.org/licenses/LICENSE-2.0%0A * %0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A * #L%25%0A */%0A
 package 

Add missing license header