Package se.dannej.fakehttpserver.integrationtests

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

/*
* 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.junit.Assert.assertThat;
import static se.dannej.fakehttpserver.expect.ResponseActions.sendContent;
import static se.dannej.fakehttpserver.expect.ResponseActions.sendContentType;
import static se.dannej.fakehttpserver.expect.matcher.RequestMatchers.content;
import static se.dannej.fakehttpserver.expect.matcher.RequestMatchers.contentType;
import static se.dannej.fakehttpserver.expect.matcher.RequestMatchers.path;
import static se.dannej.fakehttpserver.integrationtests.ClientResponseMatchers.hasStatus;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import se.dannej.fakehttpserver.FakeHttpServer;
import se.dannej.fakehttpserver.FakeHttpServletRequest;
import se.dannej.fakehttpserver.FakeHttpServletResponse;
import se.dannej.fakehttpserver.JettyFakeServer;
import se.dannej.fakehttpserver.expect.Expectations;
import se.dannej.fakehttpserver.expect.ResponseAction;
import se.dannej.fakehttpserver.junit.FakeServerRule;

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

public class FullRunWithTest {
  private static final String EXPECTED_PATH = "/";
  private static final String UNEXPECTED_PATH = "/asdf";
  private static final String CONTENT_TYPE = "application/json";
  private static final String CONTENT = "hey";
 
  @Rule public final FakeServerRule rule = new FakeServerRule();
  private FakeHttpServer server = new JettyFakeServer();
  private FakeServerCaller caller = new FakeServerCaller(server);
 
  @Before
  public void before() {
    server.start();
  }
 
  @Test
  public void exactlyOnePost() {

    new Expectations() {{
      oneOf(server).post();
    }};
   
    caller.resource().post();
  }
 
  @Test
  public void exactlyOneGet() {

    new Expectations() {{
      oneOf(server).get();
    }};
   
    caller.resource().get(ClientResponse.class);
  }

  @Test
  public void exactlyOneHead() {

    new Expectations() {{
      oneOf(server).head();
    }};
   
    caller.resource().head();
  }

  @Test
  public void exactlyOnePut() {

    new Expectations() {{
      oneOf(server).put();
    }};
   
    caller.resource().put();
  }
 
  @Test
  public void exactlyOneOptions() {

    new Expectations() {{
      oneOf(server).options();
    }};
   
    caller.resource().options(ClientResponse.class);
  }

  @Test
  public void exactlyOneDelete() {

    new Expectations() {{
      oneOf(server).delete();
    }};
   
    caller.resource().delete();
  }
 
  @Test
  public void executesGivenResponseActionsForGivenExpectations() {

    new Expectations() {{
      oneOf(server).post().with(path(EXPECTED_PATH), contentType(CONTENT_TYPE), content(CONTENT));
        will(
          sendContentType(CONTENT_TYPE),
          sendContent("[1]"));
    }};
   
    ClientResponse response = caller.resource()
      .path(EXPECTED_PATH)
      .type(CONTENT_TYPE)
      .post(ClientResponse.class, CONTENT);
   
    assertThat(response, hasStatus(200));
    assertThat(response, ClientResponseMatchers.hasContentType(CONTENT_TYPE));
    assertThat(response, ClientResponseMatchers.hasContent("[1]"));
  }

  @Test
  public void stopsExecutingExpectationsOnceOneIsMet() {

    new Expectations() {{
      oneOf(server).post().with(path(EXPECTED_PATH));
      allowing(server).post();
        will(throwException());
    }};
   
    caller.resource().path(EXPECTED_PATH).post();
  }

  @Test
  public void withoutNegatesMatchers() {

    new Expectations() {{
      oneOf(server).post().without(path(EXPECTED_PATH));
      allowing(server).post();
        will(throwException());
    }};
   
    caller.resource().path(UNEXPECTED_PATH).post();
  }


  private ResponseAction throwException() {
    return new ResponseAction() {
      @Override
      public void applyTo(FakeHttpServletRequest request, FakeHttpServletResponse response) {
        throw new RuntimeException();
      }
    };
  }
}
TOP

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

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.