Skip to content

Instantly share code, notes, and snippets.

Created March 21, 2011 15:21
What would you like to do?
public class DemarreurDeSuite extends DoFixture {
private static final Logger logger = Logger.getLogger(DemarreurDeSuite.class);
public boolean demarre() throws Exception {"demarrage de tous les mock serveurs");
demarreOptionsClient();"fin demarrage de tous les mock serveurs");
return true;
public boolean arrete() throws Exception {"arret de tous les mock serveurs");
arreteOptionsClient();"fin arret de tous les mock serveurs");
return true;
/* implementation triviale */
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);
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; }
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 {
@Test public void urlAppeleeParLeService() throws Exception {
ServiceQuiTapeSurHttp service =
new ServiceQuiTapeSurHttp("", PORT);
assertEquals("url attendue (avec param)"
// plein d'autres tests qui utilisent ce serveur HTTP
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 */
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;
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment