Skip to content
This repository has been archived by the owner on Jun 14, 2018. It is now read-only.

Commit

Permalink
Adding Filter test case
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocarvalho777 committed Oct 20, 2017
1 parent e96c9e4 commit 7d99a7e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public void happyPathTest() {
response.then().statusCode(200).body("timestamp", notNullValue()).body("echoText", equalTo("is there anybody out there?"));
}

@Test
public void filterTest() {
Response response = given().body("is there anybody out there?").header("ping", "ping").post("/echo");
response.then().statusCode(200).body("timestamp", notNullValue()).body("echoText", equalTo("is there anybody out there?")).header("pong", equalTo("pong"));
}

@Test
public void invalidUriPathTest() {
// Notice "eco" is supposed to result in 404
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.sample.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;

/**
* Custom container request filter just to
* exercise Spring beans as providers and,
* specifically, in this case, as a filter.
*
* @author Fabio Carvalho ([email protected] or [email protected])
*/
@Component
@Provider
public class CustomContainerResponseFilter implements ContainerResponseFilter {

@Autowired
private CustomSingletonBean customSingletonBean;

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {

// This will cause a NPE if this bean couldn't be injected,
// and that is all we want to check. No need for assertions here
customSingletonBean.amIAlive();

// Checks if request has a HTTP header named "ping".
// If it does, adds an HTTP header named "pong" to the response.
// The header value is irrelevant.
if(requestContext.getHeaderString("ping") != null) {
responseContext.getHeaders().add("pong", "pong");
}
}

}

0 comments on commit 7d99a7e

Please sign in to comment.