@Test public void shouldReturnEmptyEncryptionKeyIfApiReturns401Error() throws Exception { MockResponse anonymousCookie = MockResponseFactory.create200() .setBody("6.1") .addHeader("Set-Cookie", "cookie1=12"); String malformedJson = "{Error: Key generation is off}"; MockResponse encryptionKey = MockResponseFactory.create401() .setBody(malformedJson); mWebMockRule.enqueue(anonymousCookie); mWebMockRule.enqueue(encryptionKey); EncryptionKey keyResponse = apiUnderTest.requestEncryptionMetadata(); assertThat(keyResponse.isAvailable(), is(false)); }
@NotNull public EncryptionKey requestEncryptionMetadata() throws IOException, HttpException { HttpUrl url2 = mNetworkClient.getBaseUrl().resolve("GetEncryptionKey"); Request request2 = new Request.Builder() .addHeader("Accept", "application/json; charset=UTF-8") .url(url2) .get() .build(); try { Response response = mNetworkClient.makeRawCall(request2); int code = response.code(); if (code >= 200 && code < 300) { return mNetworkClient.deserializeJson(response, EncryptionKey.class); } else { return EncryptionKey.empty(); } } catch (JsonSyntaxException ex) { return EncryptionKey.empty(); } }
AuthRestApi { @NotNull public EncryptionKey requestEncryptionMetadata() throws IOException, HttpException { HttpUrl url2 = mNetworkClient.getBaseUrl().resolve("GetEncryptionKey"); Request request2 = new Request.Builder() .addHeader("Accept", "application/json; charset=UTF-8") .url(url2) .get() .build(); try { Response response = mNetworkClient.makeRawCall(request2); int code = response.code(); if (code >= 200 && code < 300) { return mNetworkClient.deserializeJson(response, EncryptionKey.class); } else { return EncryptionKey.empty(); } } catch (JsonSyntaxException ex) { return EncryptionKey.empty(); } } }
AuthRestApi { @NotNull public EncryptionKey requestEncryptionMetadata() throws IOException, HttpException { HttpUrl url2 = mNetworkClient.getBaseUrl().resolve("GetEncryptionKey"); Request request2 = new Request.Builder() .addHeader("Accept", "application/json; charset=UTF-8") .url(url2) .get() .build(); try { Response response = mNetworkClient.makeRawCall(request2); int code = response.code(); if (code >= 200 && code < 300) { return mNetworkClient.deserializeJson(response, EncryptionKey.class); } else { return EncryptionKey.empty(); } } catch (JsonSyntaxException ex) { return EncryptionKey.empty(); } } AuthRestApi(@NotNull NetworkClient client); }
AuthRestApi { @NotNull public EncryptionKey requestEncryptionMetadata() throws IOException, HttpException { HttpUrl url2 = mNetworkClient.getBaseUrl().resolve("GetEncryptionKey"); Request request2 = new Request.Builder() .addHeader("Accept", "application/json; charset=UTF-8") .url(url2) .get() .build(); try { Response response = mNetworkClient.makeRawCall(request2); int code = response.code(); if (code >= 200 && code < 300) { return mNetworkClient.deserializeJson(response, EncryptionKey.class); } else { return EncryptionKey.empty(); } } catch (JsonSyntaxException ex) { return EncryptionKey.empty(); } } AuthRestApi(@NotNull NetworkClient client); void springAuth(@NotNull final String username,
                           @NotNull final String password,
                           final String organization,
                           final Map<String, String> params); @NotNull EncryptionKey requestEncryptionMetadata(); }
AuthRestApi { @NotNull public EncryptionKey requestEncryptionMetadata() throws IOException, HttpException { HttpUrl url2 = mNetworkClient.getBaseUrl().resolve("GetEncryptionKey"); Request request2 = new Request.Builder() .addHeader("Accept", "application/json; charset=UTF-8") .url(url2) .get() .build(); try { Response response = mNetworkClient.makeRawCall(request2); int code = response.code(); if (code >= 200 && code < 300) { return mNetworkClient.deserializeJson(response, EncryptionKey.class); } else { return EncryptionKey.empty(); } } catch (JsonSyntaxException ex) { return EncryptionKey.empty(); } } AuthRestApi(@NotNull NetworkClient client); void springAuth(@NotNull final String username,
                           @NotNull final String password,
                           final String organization,
                           final Map<String, String> params); @NotNull EncryptionKey requestEncryptionMetadata();  }