Debian Codegraph @

contribs/jersey-oauth/oauth-tests/src/test/java/com/sun/jersey/oauth/tests/Client.java

raw
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can
10 * obtain a copy of the License at
11 * http://glassfish.java.net/public/CDDL+GPL_1_1.html
12 * or packager/legal/LICENSE.txt. See the License for the specific
13 * language governing permissions and limitations under the License.
14 *
15 * When distributing the software, include this License Header Notice in each
16 * file and include the License file at packager/legal/LICENSE.txt.
17 *
18 * GPL Classpath Exception:
19 * Oracle designates this particular file as subject to the "Classpath"
20 * exception as provided by Oracle in the GPL Version 2 section of the License
21 * file that accompanied this code.
22 *
23 * Modifications:
24 * If applicable, add the following below the License Header, with the fields
25 * enclosed by brackets [] replaced by your own identifying information:
26 * "Portions Copyright [year] [name of copyright owner]"
27 *
28 * Contributor(s):
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package com.sun.jersey.oauth.tests;
42
43 import javax.ws.rs.ext.Providers;
44 import junit.framework.Assert;
45 import com.sun.jersey.api.client.WebResource;
46 import com.sun.jersey.api.client.filter.LoggingFilter;
47 import com.sun.jersey.core.util.MultivaluedMapImpl;
48 import com.sun.jersey.oauth.client.OAuthClientFilter;
49 import com.sun.jersey.oauth.signature.OAuthSecrets;
50 import com.sun.jersey.oauth.signature.OAuthParameters;
51
52 class Client extends Assert {
53
54 /**
55 * Executes the client tests against the running server.
56 */
57 public static void execute(String host, int port) {
58
59 String base = "http://" + host + ":" + port;
60
61 com.sun.jersey.api.client.Client client = com.sun.jersey.api.client.Client.create();
62 client.addFilter(new LoggingFilter());
63 Providers providers = client.getProviders();
64
65 // baseline for requests
66 OAuthSecrets secrets = new OAuthSecrets().consumerSecret("kd94hf93k423kf44");
67 OAuthParameters params = new OAuthParameters().consumerKey("dpf43f3p2l4k3l03").
68 signatureMethod("PLAINTEXT").version("1.0");
69 OAuthClientFilter filter = new OAuthClientFilter(providers, params, secrets);
70 WebResource resource;
71 String response;
72
73 params.timestamp("1191242090").nonce("hsu94j3884jdopsl");
74 resource = client.resource(base + "/request_token");
75 resource.addFilter(filter);
76 response = resource.post(String.class);
77 assertEquals(response, "oauth_token=hh5s93j4hdidpola&oauth_token_secret=hdhd0244k9j7ao03");
78
79 secrets.tokenSecret("hdhd0244k9j7ao03");
80 params.token("hh5s93j4hdidpola").timestamp("1191242092").nonce("dji430splmx33448");
81 resource = client.resource(base + "/access_token");
82 resource.addFilter(filter);
83 response = resource.post(String.class);
84 assertEquals(response, "oauth_token=nnch734d00sl2jdk&oauth_token_secret=pfkkdhi9sl3r4s00");
85
86 secrets.tokenSecret("pfkkdhi9sl3r4s00");
87 params.token("nnch734d00sl2jdk").signatureMethod("HMAC-SHA1").timestamp("1191242096").
88 nonce("kllo9940pd9333jh");
89 MultivaluedMapImpl queryParams = new MultivaluedMapImpl();
90 queryParams.add("file", "vacation.jpg");
91 queryParams.add("size", "original");
92 resource = client.resource(base + "/photos").queryParams(queryParams);
93 resource.addFilter(filter);
94 response = resource.get(String.class);
95 assertEquals(response, "PHOTO");
96 }
97 }
98