001/* 002 * acme4j - Java ACME client 003 * 004 * Copyright (C) 2015 Richard "Shred" Körber 005 * http://acme4j.shredzone.org 006 * 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 013 */ 014package org.shredzone.acme4j.challenge; 015 016import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; 017import static org.assertj.core.api.Assertions.assertThat; 018import static org.junit.jupiter.api.Assertions.assertThrows; 019import static org.shredzone.acme4j.toolbox.TestUtils.getJSON; 020 021import org.junit.jupiter.api.Test; 022import org.shredzone.acme4j.Login; 023import org.shredzone.acme4j.Status; 024import org.shredzone.acme4j.exception.AcmeProtocolException; 025import org.shredzone.acme4j.toolbox.JSONBuilder; 026import org.shredzone.acme4j.toolbox.TestUtils; 027 028/** 029 * Unit tests for {@link Http01Challenge}. 030 */ 031public class Http01ChallengeTest { 032 private static final String TOKEN = 033 "rSoI9JpyvFi-ltdnBW0W1DjKstzG7cHixjzcOjwzAEQ"; 034 private static final String KEY_AUTHORIZATION = 035 "rSoI9JpyvFi-ltdnBW0W1DjKstzG7cHixjzcOjwzAEQ.HnWjTDnyqlCrm6tZ-6wX-TrEXgRdeNu9G71gqxSO6o0"; 036 037 private final Login login = TestUtils.login(); 038 039 /** 040 * Test that {@link Http01Challenge} generates a correct authorization key. 041 */ 042 @Test 043 public void testHttpChallenge() { 044 var challenge = new Http01Challenge(login, getJSON("httpChallenge")); 045 046 assertThat(challenge.getType()).isEqualTo(Http01Challenge.TYPE); 047 assertThat(challenge.getStatus()).isEqualTo(Status.PENDING); 048 assertThat(challenge.getToken()).isEqualTo(TOKEN); 049 assertThat(challenge.getAuthorization()).isEqualTo(KEY_AUTHORIZATION); 050 051 var response = new JSONBuilder(); 052 challenge.prepareResponse(response); 053 054 assertThatJson(response.toString()).isEqualTo("{}"); 055 } 056 057 /** 058 * Test that an exception is thrown if there is no token. 059 */ 060 @Test 061 public void testNoTokenSet() { 062 assertThrows(AcmeProtocolException.class, () -> { 063 Http01Challenge challenge = new Http01Challenge(login, getJSON("httpNoTokenChallenge")); 064 challenge.getToken(); 065 }); 066 } 067 068}