Package se.dannej.fakehttpserver.integrationtests

Source Code of se.dannej.fakehttpserver.integrationtests.JettyFakeServerTest

/*
* Copyright 2011 Daniel Josefsson
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package se.dannej.fakehttpserver.integrationtests;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static se.dannej.fakehttpserver.expect.Expectations.oneOf;
import static se.dannej.fakehttpserver.expect.Expectations.will;
import static se.dannej.fakehttpserver.expect.ResponseActions.sendStatus;
import static se.dannej.fakehttpserver.expect.matcher.RequestMatchers.content;
import static se.dannej.fakehttpserver.integrationtests.ClientResponseMatchers.hasStatus;

import java.io.IOException;

import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.hamcrest.StringDescription;
import org.junit.After;
import org.junit.Test;

import se.dannej.fakehttpserver.FakeHttpServer;
import se.dannej.fakehttpserver.FakeServer;
import se.dannej.fakehttpserver.JettyFakeServer;
import se.dannej.fakehttpserver.expect.Expectations;

import com.sun.jersey.api.client.ClientResponse;

public class JettyFakeServerTest {
  private static final String VALID_PATH = "/";
  private static final String VALID_CONTENT_TYPE = "plain/text";
  private final FakeHttpServer server = new JettyFakeServer();
  private final FakeServerCaller caller = new FakeServerCaller(server);
 
  @After
  public void shutdown() {
    server.stop();
  }
 
  @Test
  public void delegatesRequestsToAddedHandlers() {
    new Expectations() {{
      oneOf(server).post();
        will(sendStatus(200));
    }};
   
    server.start();
   
    ClientResponse response = caller.resource().path(VALID_PATH).post(ClientResponse.class);
   
    assertThat(response, hasStatus(200));
  }
 
  @Test
  public void isSatisfiedReturnsTrueWhenExpectationsAreMet() {
    new Expectations() {{
      oneOf(server).post();
    }};
     
    server.start();
   
    caller.resource().path(VALID_PATH).post();
   
    assertThat(server, is(satisfied()));
  }

  @Test
  public void isSatisfiedReturnsFalseWhenExpectationsAreNotMet() throws IOException {
    oneOf(server).post();
      will(sendStatus(200));
     
    server.start();
   
    assertThat(server, is(not(satisfied())));
  }
 
  @Test
  public void isSatisfiedReturnsFalseWhenUnexpectedCallReceived() throws IOException {
    server.start();

    caller.resource().path(VALID_PATH).post(ClientResponse.class);
   
    assertThat(server, is(not(satisfied())));
  }
 
  @Test
  public void allowsMoreThanOneExpectationAssertingContent() {
    final String expectedContent = "content-match";
   
    new Expectations() {{
      allowing(server).post().with(content("content-mismatch"));
        will(sendStatus(200));
      oneOf(server).post().with(content(expectedContent));
        will(sendStatus(200));
    }};
   
    server.start();
   
    ClientResponse response = caller.resource()
      .path(VALID_PATH)
      .type(VALID_CONTENT_TYPE)
      .post(ClientResponse.class, expectedContent.getBytes());
   
    assertThat(response, hasStatus(200));
  }
 
 
  private Matcher<FakeServer> satisfied() {
    return new FeatureMatcher<FakeServer, Boolean>(Matchers.is(true), "satisfied", "satisfied") {
      @Override
      protected Boolean featureValueOf(FakeServer actual) {
        return actual.isSatisfied(new StringDescription());
      }
    };
  }
}
TOP

Related Classes of se.dannej.fakehttpserver.integrationtests.JettyFakeServerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.