Search in sources :

Example 11 with AuthorizationValue

use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.

the class RemoteUrl method urlToString.

public static String urlToString(String url, List<AuthorizationValue> auths) throws Exception {
    InputStream is = null;
    BufferedReader br = null;
    try {
        final URL inUrl = new URL(cleanUrl(url));
        final List<AuthorizationValue> query = new ArrayList<>();
        final List<AuthorizationValue> header = new ArrayList<>();
        if (auths != null) {
            for (AuthorizationValue auth : auths) {
                if ("query".equals(auth.getType())) {
                    appendValue(inUrl, auth, query);
                } else if ("header".equals(auth.getType())) {
                    appendValue(inUrl, auth, header);
                }
            }
        }
        final URLConnection conn;
        if (!query.isEmpty()) {
            final URI inUri = inUrl.toURI();
            final StringBuilder newQuery = new StringBuilder(inUri.getQuery() == null ? "" : inUri.getQuery());
            for (AuthorizationValue item : query) {
                if (newQuery.length() > 0) {
                    newQuery.append("&");
                }
                newQuery.append(URLEncoder.encode(item.getKeyName(), UTF_8.name())).append("=").append(URLEncoder.encode(item.getValue(), UTF_8.name()));
            }
            conn = new URI(inUri.getScheme(), inUri.getAuthority(), inUri.getPath(), newQuery.toString(), inUri.getFragment()).toURL().openConnection();
        } else {
            conn = inUrl.openConnection();
        }
        CONNECTION_CONFIGURATOR.process(conn);
        for (AuthorizationValue item : header) {
            conn.setRequestProperty(item.getKeyName(), item.getValue());
        }
        conn.setRequestProperty("Accept", ACCEPT_HEADER_VALUE);
        conn.setRequestProperty("User-Agent", USER_AGENT_HEADER_VALUE);
        conn.connect();
        InputStream in = conn.getInputStream();
        StringBuilder contents = new StringBuilder();
        BufferedReader input = new BufferedReader(new InputStreamReader(in, UTF_8));
        for (int i = 0; i != -1; i = input.read()) {
            char c = (char) i;
            if (!Character.isISOControl(c)) {
                contents.append((char) i);
            }
            if (c == '\n') {
                contents.append('\n');
            }
        }
        in.close();
        return contents.toString();
    } catch (javax.net.ssl.SSLProtocolException e) {
        LOGGER.warn("there is a problem with the target SSL certificate");
        LOGGER.warn("**** you may want to run with -Djsse.enableSNIExtension=false\n\n");
        LOGGER.error("unable to read", e);
        throw e;
    } catch (Exception e) {
        LOGGER.error("unable to read", e);
        throw e;
    } finally {
        if (is != null) {
            is.close();
        }
        if (br != null) {
            br.close();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) URI(java.net.URI) javax.net.ssl(javax.net.ssl) URL(java.net.URL) URLConnection(java.net.URLConnection) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AuthorizationValue(io.swagger.models.auth.AuthorizationValue) BufferedReader(java.io.BufferedReader)

Example 12 with AuthorizationValue

use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.

the class NetworkReferenceTests method testIssue328.

@Test
public void testIssue328() throws Exception {
    new Expectations() {

        {
            remoteUrl.urlToString("http://localhost:8080/resources/swagger/issue-328.yaml", new ArrayList<AuthorizationValue>());
            result = issue_328_yaml;
            remoteUrl.urlToString("http://localhost:8080/resources/swagger/issue-328-events.yaml", new ArrayList<AuthorizationValue>());
            result = issue_328_events_yaml;
            remoteUrl.urlToString("http://localhost:8080/resources/swagger/common/issue-328-paging.yaml", new ArrayList<AuthorizationValue>());
            result = issue_328_paging_yaml;
            remoteUrl.urlToString("http://localhost:8080/resources/swagger/common/common2/issue-328-bar.yaml", new ArrayList<AuthorizationValue>());
            result = issue_328_bar_yaml;
        }
    };
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("http://localhost:8080/resources/swagger/issue-328.yaml", null, true);
    assertNotNull(result.getSwagger());
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger.getPath("/events"));
    assertNotNull(swagger.getDefinitions().get("StatusResponse"));
    assertNotNull(swagger.getDefinitions().get("Paging"));
    assertNotNull(swagger.getDefinitions().get("Foobar"));
}
Also used : Expectations(mockit.Expectations) AuthorizationValue(io.swagger.models.auth.AuthorizationValue) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Swagger(io.swagger.models.Swagger) Test(org.testng.annotations.Test)

Example 13 with AuthorizationValue

use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.

the class NetworkReferenceTests method testIssue330.

@Test
public void testIssue330() throws Exception {
    new Expectations() {

        {
            remoteUrl.urlToString("http://server1/resources/swagger.yaml", new ArrayList<AuthorizationValue>());
            result = issue_330_yaml;
            remoteUrl.urlToString("http://server1/resources/common/paging.yaml", new ArrayList<AuthorizationValue>());
            result = issue_330_paging_yaml;
            remoteUrl.urlToString("http://server1/resources/common/users.yaml", new ArrayList<AuthorizationValue>());
            result = issue_330_users_yaml;
            remoteUrl.urlToString("http://server2/resources/common/entities.yaml", new ArrayList<AuthorizationValue>());
            result = issue_330_entities_yaml;
        }
    };
    SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("http://server1/resources/swagger.yaml", null, true);
    assertNotNull(result.getSwagger());
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger.getPath("/events"));
    assertNotNull(swagger.getDefinitions().get("Address"));
    assertNotNull(swagger.getDefinitions().get("Paging"));
    assertNotNull(swagger.getDefinitions().get("users"));
    assertNotNull(swagger.getDefinitions().get("Phone"));
}
Also used : Expectations(mockit.Expectations) AuthorizationValue(io.swagger.models.auth.AuthorizationValue) SwaggerDeserializationResult(io.swagger.parser.util.SwaggerDeserializationResult) Swagger(io.swagger.models.Swagger) Test(org.testng.annotations.Test)

Example 14 with AuthorizationValue

use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.

the class RemoteUrlTest method testSkippedHeader.

@Test
public void testSkippedHeader() throws Exception {
    final String expectedBody = setupStub();
    final String headerName = "Authorization";
    final String headerValue = "foobar";
    final AuthorizationValue authorizationValue = new HostAuthorizationValue(SOME_HOST, headerName, headerValue, "header");
    final String actualBody = RemoteUrl.urlToString(getUrl(), Arrays.asList(authorizationValue));
    assertEquals(actualBody, expectedBody);
    verify(getRequestedFor(urlEqualTo("/v2/pet/1")).withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER)).withoutHeader(headerName));
}
Also used : AuthorizationValue(io.swagger.models.auth.AuthorizationValue) Test(org.testng.annotations.Test)

Example 15 with AuthorizationValue

use of io.swagger.models.auth.AuthorizationValue in project swagger-parser by swagger-api.

the class RemoteUrlTest method testAuthorizationHeader.

@Test
public void testAuthorizationHeader() throws Exception {
    final String expectedBody = setupStub();
    final String headerName = "Authorization";
    final String headerValue = "foobar";
    final AuthorizationValue authorizationValue = new AuthorizationValue(headerName, headerValue, "header");
    final String actualBody = RemoteUrl.urlToString(getUrl(), Arrays.asList(authorizationValue));
    assertEquals(actualBody, expectedBody);
    verify(getRequestedFor(urlEqualTo("/v2/pet/1")).withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER)).withHeader(headerName, equalTo(headerValue)));
}
Also used : AuthorizationValue(io.swagger.models.auth.AuthorizationValue) Test(org.testng.annotations.Test)

Aggregations

AuthorizationValue (io.swagger.models.auth.AuthorizationValue)18 Test (org.testng.annotations.Test)15 SwaggerDeserializationResult (io.swagger.parser.util.SwaggerDeserializationResult)10 Expectations (mockit.Expectations)8 Swagger (io.swagger.models.Swagger)6 SwaggerParser (io.swagger.parser.SwaggerParser)2 ArrayList (java.util.ArrayList)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ModelImpl (io.swagger.models.ModelImpl)1 Path (io.swagger.models.Path)1 Response (io.swagger.models.Response)1 Parameter (io.swagger.models.parameters.Parameter)1 Property (io.swagger.models.properties.Property)1 RefProperty (io.swagger.models.properties.RefProperty)1 StringProperty (io.swagger.models.properties.StringProperty)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1