This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DemarreurDeSuite extends DoFixture { | |
private static final Logger logger = Logger.getLogger(DemarreurDeSuite.class); | |
public boolean demarre() throws Exception { | |
logger.info("demarrage de tous les mock serveurs"); | |
demarreStreamer(); | |
demarrePublicite(); | |
demarrePaiement(); | |
demarreOptionsClient(); | |
logger.info("fin demarrage de tous les mock serveurs"); | |
return true; | |
} | |
public boolean arrete() throws Exception { | |
logger.info("arret de tous les mock serveurs"); | |
arreteStreamer(); | |
arretePublicite(); | |
arretePaiement(); | |
arreteOptionsClient(); | |
logger.info("fin arret de tous les mock serveurs"); | |
return true; | |
} | |
/* implementation triviale */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MockHttpServer { | |
public enum HttpMethod {GET, POST, PUT, DELETE}; | |
// je ne peux montrer ici le code de cette classe : | |
private final Serveur serveur; | |
public MockHttpServer(final int port) throws IOException { | |
this(port, "defaultId"); | |
} | |
public MockHttpServer(final int port) throws IOException { | |
this(port, TraiteurDeReponseFactory.repondOk()); | |
} | |
public MockHttpServer(final int port, | |
TraiteurDeReponse traiteurDeReponse) throws IOException { | |
serveur = new Serveur(port, traiteurDeReponse); | |
Executors.newSingleThreadExecutor().execute(serveur); | |
} | |
public void close() { serveur.close(); } | |
public String getLastUrl() { return serveur.lastUrl; } | |
public String getLastBody() { return serveur.lastBody; } | |
public Map<String, String> getLastHeaders() { return serveur.lastHeaders; } | |
public HttpMethod getLastMethod() { return serveur.lastMethod; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TesterUnServiceHttp { | |
private static MockHttpServer serveurDeTest; | |
private final static int PORT = 8084; | |
@BeforeClass public static void demarreLeMockServeurHttp() | |
throws Exception { | |
// la creation du serveur provoque son démarrage | |
serveurDeTest = new MockHttpServer(PORT); | |
} | |
@AfterClass public static void arreteLeMockServeurHttp() | |
throws Exception { | |
serveurDeTest.close(); | |
} | |
@Test public void urlAppeleeParLeService() throws Exception { | |
serveurDeTest.setReponse(TraiteurDeReponseFactory.repondOk()); | |
ServiceQuiTapeSurHttp service = | |
new ServiceQuiTapeSurHttp("127.0.0.1", PORT); | |
assertEquals("url attendue (avec param)" | |
serveurDeTest.getLastUrl()); | |
} | |
// plein d'autres tests qui utilisent ce serveur HTTP | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TraiteurDeReponse { | |
private final static String CRLF = "\r\n"; | |
public int statusHttp; | |
public final String corpsDeLaReponse; | |
public final Map<String, String> headers; | |
public final long delaiDAttenteMillis; | |
public TraiteurDeReponse(int statusHttp) { | |
this(statusHttp, "pas utilise", 0); | |
} | |
public TraiteurDeReponse(int statusHttp, String corpsDeLaReponse) { | |
this(statusHttp, corpsDeLaReponse, 0); | |
} | |
public TraiteurDeReponse(int statusHttp, String corpsDeLaReponse, | |
long delaiDAttenteMillis) { | |
this.statusHttp = statusHttp; | |
this.corpsDeLaReponse = corpsDeLaReponse; | |
this.headers = new HashMap<String, String>(); | |
this.delaiDAttenteMillis = delaiDAttenteMillis; | |
} | |
public void setContentType(String contentType) { | |
addHeader("Content-Type", contentType); | |
} | |
public void addHeader(String clef, String valeur) { | |
headers.put(clef, valeur); | |
} | |
/* du code que je ne peux ouvrir pour le moment */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TraiteurDeReponseFactory { | |
public static TraiteurDeReponse repondBadRequest() { | |
return new TraiteurDeReponse(HttpStatus.SC_BAD_REQUEST, | |
"Bad Request"); | |
} | |
public static TraiteurDeReponse repondOk() { | |
return new TraiteurDeReponse(HttpStatus.SC_OK, "OK"); | |
} | |
public static TraiteurDeReponse redirect(String urlDestination) { | |
final TraiteurDeReponse redirect = | |
new TraiteurDeReponse(HttpStatus.SC_MOVED_TEMPORARILY, ""); | |
redirect.addHeader("Location", urlDestination); | |
return redirect; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cf http://www.barreverte.fr/comment-tester-les-interractions-avec-le-monde-exterieur-via-http pour une utilisation de ces bouts de classes