예제 #1
0
  @Test
  public void keyShouldResetClientWithNewKey() throws Exception, InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(200));

    Tinify.setKey("abcde");
    Tinify.client();
    Tinify.setKey("fghij");
    Tinify.client().request(Client.Method.GET, "/");

    RecordedRequest request = server.takeRequest(5, TimeUnit.SECONDS);

    String credentials = new String(Base64.encodeBase64(("api:fghij").getBytes()));
    assertEquals("Basic " + credentials, request.getHeader("Authorization"));
  }
예제 #2
0
  @Test
  public void fromBufferShouldReturnSource() {
    Tinify.setKey("valid");

    server.enqueue(
        new MockResponse()
            .setResponseCode(201)
            .addHeader("Location", "https://api.tinify.com/some/location")
            .addHeader("Compression-Count", 12));

    assertThat(Tinify.fromBuffer("png file".getBytes()), isA(Source.class));
  }
예제 #3
0
  @Test
  public void appIdentifierShouldResetClientWithNewAppIdentifier() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(200));

    Tinify.setKey("abcde");
    Tinify.setAppIdentifier("MyApp/1.0");
    Tinify.client();
    Tinify.setAppIdentifier("MyApp/2.0");
    Tinify.client().request(Client.Method.GET, "/");

    RecordedRequest request = server.takeRequest(5, TimeUnit.SECONDS);
    assertEquals(Client.USER_AGENT + " MyApp/2.0", request.getHeader("User-Agent"));
  }
예제 #4
0
  @Test
  public void validateWithValidKeyShouldReturnTrue() throws InterruptedException {
    server.enqueue(
        new MockResponse()
            .setResponseCode(400)
            .setBody("{'error':'Input missing','message':'No input'}"));

    Tinify.setKey("valid");
    assertThat(Tinify.validate(), is(true));

    RecordedRequest request = server.takeRequest();
    assertEquals("POST /shrink HTTP/1.1", request.getRequestLine());
    assertEquals(0, request.getBody().size());
  }
예제 #5
0
  @Test
  public void fromFileShouldReturnSource() throws IOException, URISyntaxException {
    Tinify.setKey("valid");

    server.enqueue(
        new MockResponse()
            .setResponseCode(201)
            .addHeader("Location", "https://api.tinify.com/some/location")
            .addHeader("Compression-Count", 12));

    String filePath =
        Paths.get(getClass().getResource("/dummy.png").toURI()).toAbsolutePath().toString();
    assertThat(Tinify.fromFile(filePath), isA(Source.class));
  }
예제 #6
0
  @Test(expected = AccountException.class)
  public void validateWithErrorShouldThrowException() throws InterruptedException {
    server.enqueue(
        new MockResponse()
            .setResponseCode(401)
            .setBody("{'error':'Unauthorized','message':'Credentials are invalid'}"));

    Tinify.setKey("invalid");
    Tinify.validate();

    RecordedRequest request = server.takeRequest();
    assertEquals("POST /shrink HTTP/1.1", request.getRequestLine());
    assertEquals(0, request.getBody().size());
  }
예제 #7
0
  @Test
  public void validateWithLimitedKeyShouldReturnTrue() throws InterruptedException {
    server.enqueue(
        new MockResponse()
            .setResponseCode(429)
            .setBody(
                "{'error':'Too many requests','message':'Your monthly limit has been exceeded'}"));

    Tinify.setKey("valid");
    assertThat(Tinify.validate(), is(true));

    RecordedRequest request = server.takeRequest();
    assertEquals("POST /shrink HTTP/1.1", request.getRequestLine());
    assertEquals(0, request.getBody().size());
  }
예제 #8
0
 @Test
 public void clientWithKeyShouldReturnClient() {
   Tinify.setKey("abcde");
   assertThat(Tinify.client(), isA(Client.class));
 }
예제 #9
0
 @After
 public void tearDown() throws IOException {
   Tinify.setKey(null);
   server.shutdown();
 }