net.paslavsky.springrest.HttpHeadersHelperTest.java Source code

Java tutorial

Introduction

Here is the source code for net.paslavsky.springrest.HttpHeadersHelperTest.java

Source

/*
 * Copyright (c) 2014 Andrey Paslavsky.
 *
 * 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 net.paslavsky.springrest;

import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.net.URI;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Tests for the {@link HttpHeadersHelper}
 *
 * @author Andrey Paslavsky
 * @version 1.0
 */
public class HttpHeadersHelperTest {
    private HttpHeadersHelper helper;

    @BeforeMethod(firstTimeOnly = true)
    public void setUp() throws Exception {
        helper = new HttpHeadersHelper(new DefaultConversionService());
    }

    @DataProvider
    public Object[][] data() {
        DateFormat dateFormat = createDateFormat();
        final long currentTime = System.currentTimeMillis();
        final String currentTimeStr = dateFormat.format(new Date(currentTime));
        return new Object[][] { new Object[] { "Other", new Object[] { 123 }, "123" },
                new Object[] { "Other", new Object[] { "123" }, "123" },
                new Object[] { "Other", new String[] { "123" }, "123" },
                new Object[] { "Other", Arrays.asList("123"), "123" }, new Object[] { "Other", "123", "123" },
                new Object[] { "Accept", MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_VALUE },
                new Object[] { "Accept", Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML),
                        MediaType.APPLICATION_JSON_VALUE + ", " + MediaType.APPLICATION_XML_VALUE },
                new Object[] { "Accept-Charset", java.nio.charset.Charset.forName("UTF-8"), "utf-8" },
                new Object[] { "Allow", HttpMethod.GET, "GET" }, new Object[] { "Allow",
                        new TreeSet<HttpMethod>(Arrays.asList(HttpMethod.PUT, HttpMethod.POST)), "POST,PUT" },
                new Object[] { "Connection", "close", "close" },
                new Object[] { "Content-Disposition", "form-data; name=\"AttachedFile1\"; filename=\"photo-1.jpg\"",
                        "form-data; name=\"AttachedFile1\"; filename=\"photo-1.jpg\"" },
                new Object[] { "Content-Disposition", new String[] { "AttachedFile1", "photo-1.jpg" },
                        "form-data; name=\"AttachedFile1\"; filename=\"photo-1.jpg\"" },
                new Object[] { "Content-Disposition", "AttachedFile1", "form-data; name=\"AttachedFile1\"" },
                new Object[] { "Content-Length", 123l, "123" },
                new Object[] { "Content-Type", MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_VALUE },
                new Object[] { "Content-Type", MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE },
                new Object[] { "Date", currentTime, currentTimeStr },
                new Object[] { "ETag", "W/\"123456789\"", "W/\"123456789\"" },
                new Object[] { "Expires", currentTime, currentTimeStr },
                new Object[] { "If-Modified-Since", currentTime, currentTimeStr },
                new Object[] { "If-None-Match", "737060cd8c284d8af7ad3082f209582d",
                        "737060cd8c284d8af7ad3082f209582d" },
                new Object[] { "Last-Modified", currentTime, currentTimeStr },
                new Object[] { "Location", "http://example.com/", "http://example.com/" },
                new Object[] { "Location", URI.create("http://example.com/"), "http://example.com/" },
                new Object[] { "Origin", "www.a.com", "www.a.com" },
                new Object[] { "Pragma", "no-cache", "no-cache" }, new Object[] { "Upgrade",
                        "HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11", "HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11" }, };
    }

    private DateFormat createDateFormat() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        return dateFormat;
    }

    @Test(dataProvider = "data")
    public void testGetHttpHeaders(String header, Object value, String converted) throws Exception {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put(header, 0);
        HttpHeaders headers = helper.getHttpHeaders(map, new Object[] { value });
        Assert.assertNotNull(headers);
        Assert.assertEquals(headers.size(), 1);
        Assert.assertEquals(headers.get(header).get(0), converted);
    }
}