~ubuntu-branches/ubuntu/saucy/restlet/saucy

« back to all changes in this revision

Viewing changes to org.restlet.test/src/org/restlet/test/jaxrs/services/tests/HttpHeaderTest.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 16:25:45 UTC
  • Revision ID: package-import@ubuntu.com-20120611162545-5w2o0resi5y3pybc
Tags: upstream-2.0.14
ImportĀ upstreamĀ versionĀ 2.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2005-2012 Restlet S.A.S.
 
3
 * 
 
4
 * The contents of this file are subject to the terms of one of the following
 
5
 * open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
 
6
 * 1.0 (the "Licenses"). You can select the license that you prefer but you may
 
7
 * not use this file except in compliance with one of these Licenses.
 
8
 * 
 
9
 * You can obtain a copy of the Apache 2.0 license at
 
10
 * http://www.opensource.org/licenses/apache-2.0
 
11
 * 
 
12
 * You can obtain a copy of the LGPL 3.0 license at
 
13
 * http://www.opensource.org/licenses/lgpl-3.0
 
14
 * 
 
15
 * You can obtain a copy of the LGPL 2.1 license at
 
16
 * http://www.opensource.org/licenses/lgpl-2.1
 
17
 * 
 
18
 * You can obtain a copy of the CDDL 1.0 license at
 
19
 * http://www.opensource.org/licenses/cddl1
 
20
 * 
 
21
 * You can obtain a copy of the EPL 1.0 license at
 
22
 * http://www.opensource.org/licenses/eclipse-1.0
 
23
 * 
 
24
 * See the Licenses for the specific language governing permissions and
 
25
 * limitations under the Licenses.
 
26
 * 
 
27
 * Alternatively, you can obtain a royalty free commercial license with less
 
28
 * limitations, transferable or non-transferable, directly at
 
29
 * http://www.restlet.com/products/restlet-framework
 
30
 * 
 
31
 * Restlet is a registered trademark of Restlet S.A.S.
 
32
 */
 
33
 
 
34
package org.restlet.test.jaxrs.services.tests;
 
35
 
 
36
import java.io.IOException;
 
37
import java.util.ArrayList;
 
38
import java.util.Collections;
 
39
import java.util.List;
 
40
import java.util.Set;
 
41
 
 
42
import javax.ws.rs.core.Application;
 
43
 
 
44
import org.restlet.Request;
 
45
import org.restlet.Response;
 
46
import org.restlet.data.ClientInfo;
 
47
import org.restlet.data.Cookie;
 
48
import org.restlet.data.Language;
 
49
import org.restlet.data.MediaType;
 
50
import org.restlet.data.Method;
 
51
import org.restlet.data.Preference;
 
52
import org.restlet.data.Status;
 
53
import org.restlet.ext.jaxrs.internal.util.Util;
 
54
import org.restlet.representation.StringRepresentation;
 
55
import org.restlet.test.jaxrs.services.resources.HttpHeaderTestService;
 
56
 
 
57
/**
 
58
 * @author Stephan Koops
 
59
 * @see HttpHeaderTestService
 
60
 */
 
61
public class HttpHeaderTest extends JaxRsTestCase {
 
62
 
 
63
    public static void main(String[] args) throws Exception {
 
64
        new HttpHeaderTest().runServerUntilKeyPressed();
 
65
    }
 
66
 
 
67
    @Override
 
68
    protected Application getApplication() {
 
69
        return new Application() {
 
70
            @Override
 
71
            @SuppressWarnings({ "unchecked", "rawtypes" })
 
72
            public Set<Class<?>> getClasses() {
 
73
                return (Set) Collections.singleton(HttpHeaderTestService.class);
 
74
            }
 
75
        };
 
76
    }
 
77
 
 
78
    public void testAccMediaType() throws IOException {
 
79
        Response response = get("accMediaTypes", MediaType.TEXT_PLAIN);
 
80
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
81
        assertEquals("[" + MediaType.TEXT_PLAIN.toString() + "]", response
 
82
                .getEntity().getText());
 
83
 
 
84
        ClientInfo clientInfo = new ClientInfo();
 
85
        clientInfo.getAcceptedMediaTypes().add(
 
86
                new Preference<MediaType>(MediaType.TEXT_PLAIN, 0.5f));
 
87
        clientInfo.getAcceptedMediaTypes().add(
 
88
                new Preference<MediaType>(MediaType.TEXT_HTML, 0.8f));
 
89
        clientInfo.getAcceptedMediaTypes().add(
 
90
                new Preference<MediaType>(MediaType.TEXT_XML, 0.2f));
 
91
        response = get("accMediaTypes", clientInfo);
 
92
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
93
        assertEquals("[" + MediaType.TEXT_HTML.toString() + ", "
 
94
                + MediaType.TEXT_PLAIN.toString() + ", "
 
95
                + MediaType.TEXT_XML.toString() + "]", response.getEntity()
 
96
                .getText());
 
97
    }
 
98
 
 
99
    public void testCookies() throws IOException {
 
100
        final Request request = createGetRequest("cookies/cookieName");
 
101
        request.getCookies().add(new Cookie("cookieName", "cookie-value"));
 
102
        final Response response = accessServer(request);
 
103
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
104
        assertEquals("cookieName=cookie-value", response.getEntity().getText());
 
105
    }
 
106
 
 
107
    public void testHeaderParam() throws IOException {
 
108
        Request request = createGetRequest("HeaderParam");
 
109
        Util.getHttpHeaders(request).add(
 
110
                HttpHeaderTestService.TEST_HEADER_NAME, "abc");
 
111
        Response response = accessServer(request);
 
112
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
113
        assertEquals("abc", response.getEntity().getText());
 
114
 
 
115
        request = createGetRequest("HeaderParam");
 
116
        Util.getHttpHeaders(request).add(
 
117
                HttpHeaderTestService.TEST_HEADER_NAME.toLowerCase(), "abc");
 
118
        response = accessServer(request);
 
119
        sysOutEntityIfError(response);
 
120
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
121
        assertEquals("abc", response.getEntity().getText());
 
122
 
 
123
        request = createGetRequest("HeaderParam");
 
124
        Util.getHttpHeaders(request).add(
 
125
                HttpHeaderTestService.TEST_HEADER_NAME.toUpperCase(), "abc");
 
126
        response = accessServer(request);
 
127
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
128
        assertEquals("abc", response.getEntity().getText());
 
129
    }
 
130
 
 
131
    public void testHttpHeaders() throws IOException {
 
132
        Request request = createGetRequest("header/"
 
133
                + HttpHeaderTestService.TEST_HEADER_NAME);
 
134
        Util.getHttpHeaders(request).add(
 
135
                HttpHeaderTestService.TEST_HEADER_NAME, "abc");
 
136
        Response response = accessServer(request);
 
137
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
138
        assertEquals("abc", response.getEntity().getText());
 
139
 
 
140
        request = createGetRequest("header/"
 
141
                + HttpHeaderTestService.TEST_HEADER_NAME);
 
142
        Util.getHttpHeaders(request).add(
 
143
                HttpHeaderTestService.TEST_HEADER_NAME.toLowerCase(), "abc");
 
144
        response = accessServer(request);
 
145
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
146
        assertEquals("abc", response.getEntity().getText());
 
147
 
 
148
        request = createGetRequest("header/"
 
149
                + HttpHeaderTestService.TEST_HEADER_NAME);
 
150
        Util.getHttpHeaders(request).add(
 
151
                HttpHeaderTestService.TEST_HEADER_NAME.toUpperCase(), "abc");
 
152
        response = accessServer(request);
 
153
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
154
        assertEquals("abc", response.getEntity().getText());
 
155
    }
 
156
 
 
157
    public void testHttpHeadersCaseInsensitive() {
 
158
        final Response response = get("header2");
 
159
        sysOutEntityIfError(response);
 
160
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
161
    }
 
162
 
 
163
    /**
 
164
     * @see HttpHeaderTestService#getLanguage(javax.ws.rs.core.HttpHeaders)
 
165
     */
 
166
    public void testLanguage() throws IOException {
 
167
        final List<Preference<Language>> acceptedLanguages = new ArrayList<Preference<Language>>();
 
168
        acceptedLanguages.add(new Preference<Language>(Language.ENGLISH));
 
169
        final ClientInfo clientInfo = new ClientInfo();
 
170
        clientInfo.setAcceptedLanguages(acceptedLanguages);
 
171
 
 
172
        final Request request = new Request(Method.POST, createReference(
 
173
                HttpHeaderTestService.class, "language"));
 
174
        request.setClientInfo(clientInfo);
 
175
        request.setEntity(new StringRepresentation("entity", Language.ENGLISH));
 
176
        final Response response = accessServer(request);
 
177
 
 
178
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
179
        assertEquals("en", response.getEntity().getText());
 
180
    }
 
181
 
 
182
    public void testWithDefault() throws Exception {
 
183
        Request request = createGetRequest("headerWithDefault");
 
184
        Util.getHttpHeaders(request).add(
 
185
                HttpHeaderTestService.TEST_HEADER_NAME, "abc");
 
186
        Response response = accessServer(request);
 
187
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
188
        assertEquals("abc", response.getEntity().getText());
 
189
 
 
190
        request = createGetRequest("headerWithDefault");
 
191
        response = accessServer(request);
 
192
        assertEquals(Status.SUCCESS_OK, response.getStatus());
 
193
        assertEquals("default", response.getEntity().getText());
 
194
    }
 
195
}