|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2017 Intuit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + *******************************************************************************/ |
| 16 | +package com.intuit.ipp.security; |
| 17 | + |
| 18 | +import java.net.HttpURLConnection; |
| 19 | +import java.net.URI; |
| 20 | +import java.net.URL; |
| 21 | + |
| 22 | +import org.apache.http.Header; |
| 23 | +import org.apache.http.client.methods.HttpGet; |
| 24 | +import org.apache.http.client.methods.HttpRequestBase; |
| 25 | +import org.testng.Assert; |
| 26 | +import org.testng.annotations.Test; |
| 27 | + |
| 28 | +import com.intuit.ipp.interceptors.HTTPURLConnectionInterceptor; |
| 29 | +import com.intuit.ipp.util.Config; |
| 30 | +import com.intuit.ipp.util.Logger; |
| 31 | + |
| 32 | +public class OAuth2AuthorizerTest { |
| 33 | + |
| 34 | + private org.slf4j.Logger log = Logger.getLogger(); |
| 35 | + private final String URL_STRING = "http://code.intuit.com"; |
| 36 | + |
| 37 | + public String extractHeaderParams(Header[] header, |
| 38 | + HttpRequestBase requestBase) { |
| 39 | + |
| 40 | + String value = null; |
| 41 | + if (header != null) |
| 42 | + for (Header head : header) { |
| 43 | + log.debug(head.toString()); |
| 44 | + if (head.getName().equals("Authorization")) { |
| 45 | + return head.getValue().toString(); |
| 46 | + /*headerArray = head.getValue().split("=,"); |
| 47 | + headerArray = headerArray[0].split(","); |
| 48 | +
|
| 49 | + for (int counter = 0; counter < headerArray.length; counter++) { |
| 50 | + headerItemArray = headerArray[counter].split("="); |
| 51 | + if (headerItemArray[0].equals(headerName)) { |
| 52 | + value = headerItemArray[1].replace("\"", ""); |
| 53 | + return value; |
| 54 | + } |
| 55 | + }*/ |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + return value; |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void oAuthAuthorizerWithAccessTokenSecret() { |
| 64 | + |
| 65 | + String accessToken = "TestAccessToken"; |
| 66 | + Header[] header = null; |
| 67 | + HttpRequestBase requestBase = new HttpGet(); |
| 68 | + try { |
| 69 | + requestBase.setURI(new URI(URL_STRING)); |
| 70 | + OAuth2Authorizer oauthAuthorizer = new OAuth2Authorizer(accessToken); |
| 71 | + oauthAuthorizer.authorize(requestBase); |
| 72 | + |
| 73 | + header = requestBase.getAllHeaders(); |
| 74 | + Assert.assertEquals("Bearer "+accessToken,extractHeaderParams(header, requestBase)); |
| 75 | + |
| 76 | + |
| 77 | + } catch (Exception e) { |
| 78 | + |
| 79 | + log.debug(e.getMessage()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void oAuthAuthorizerExceptionTest() { |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void oAuthAuthorizerWithAccessTokenSecretHttpUrlConnection() { |
| 90 | + |
| 91 | + String accessToken = "TestAccessToken"; |
| 92 | + |
| 93 | + Config.setProperty(Config.HTTP_TRANSPORT, HTTPURLConnectionInterceptor.HTTP_URL_CONNECTION); |
| 94 | + |
| 95 | + try { |
| 96 | + URL url = new URL(URL_STRING); |
| 97 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 98 | + |
| 99 | + OAuth2Authorizer oauthAuthorizer = new OAuth2Authorizer(accessToken); |
| 100 | + |
| 101 | + oauthAuthorizer.authorize(conn); |
| 102 | + |
| 103 | + |
| 104 | + } catch (Exception e) { |
| 105 | + log.debug(e.getMessage()); |
| 106 | + Assert.fail("Exception should not happen"); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments