br.com.caelum.vraptor.controller.HttpMethod Java Examples

The following examples show how to use br.com.caelum.vraptor.controller.HttpMethod. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultRefererResultTest.java    From vraptor4 with Apache License 2.0 6 votes vote down vote up
@Test
public void whenRefererMatchesAControllerShouldForwardToIt() throws Exception {
	LogicResult logic = mock(LogicResult.class);
	RefererController controller = mock(RefererController.class);
	
	Method index = RefererController.class.getMethod("index");
	ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index);
	
	when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller");
	when(request.getContextPath()).thenReturn("/vraptor");
	when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method);
	doReturn(logic).when(result).use(logic());
	when(logic.forwardTo(RefererController.class)).thenReturn(controller);
	
	refererResult.forward();
	
	verify(logic).forwardTo(RefererController.class);
	verify(controller).index();
}
 
Example #2
Source File: DefaultRouterTest.java    From vraptor4 with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldThrowMethodNotAllowedExceptionWhenNoRoutesMatchTheURIWithGivenHttpMethod() throws Exception {
	Route route = mock(Route.class);
	when(route.canHandle(anyString())).thenReturn(true);
	when(route.getControllerMethod()).thenReturn(anyControllerMethod());
	when(route.allowedMethods()).thenReturn(EnumSet.of(HttpMethod.GET));

	router.add(route);

	try {
		router.parse("any uri", HttpMethod.DELETE, request);
		fail("MethodNotAllowedException is expected");
	} catch (MethodNotAllowedException e) {
		assertThat(e.getAllowedMethods(), is((Set<HttpMethod>)EnumSet.of(HttpMethod.GET)));
	}
}
 
Example #3
Source File: DefaultRouterTest.java    From vraptor4 with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldObeyPriorityOfRoutes() throws Exception {
	Route first = mock(Route.class);
	when(first.getControllerMethod()).thenReturn(anyControllerMethod());
	Route second = mock(Route.class);
	when(second.getControllerMethod()).thenReturn(anyControllerMethod());

	ControllerMethod method2 = second.controllerMethod(request, "second");

	router.add(second);
	router.add(first);

	when(first.getPriority()).thenReturn(Path.HIGH);
	when(second.getPriority()).thenReturn(Path.LOW);

	EnumSet<HttpMethod> get = EnumSet.of(HttpMethod.GET);
	when(first.allowedMethods()).thenReturn(get);
	when(second.allowedMethods()).thenReturn(get);

	when(first.canHandle(anyString())).thenReturn(false);
	when(second.canHandle(anyString())).thenReturn(true);

	ControllerMethod found = router.parse("anything", HttpMethod.GET, request);
	assertThat(found, is(method2));
}
 
Example #4
Source File: DefaultRouterTest.java    From vraptor4 with Apache License 2.0 6 votes vote down vote up
@Test
public void usesTheFirstRegisteredRuleMatchingThePattern() throws SecurityException, NoSuchMethodException {
	Route route = mock(Route.class);
	Route second = mock(Route.class, "second");
	when(route.getControllerMethod()).thenReturn(anyControllerMethod());
	when(second.getControllerMethod()).thenReturn(anyControllerMethod());

	when(route.canHandle("/clients/add")).thenReturn(true);
	when(second.canHandle("/clients/add")).thenReturn(true);

	EnumSet<HttpMethod> all = EnumSet.allOf(HttpMethod.class);

	when(route.allowedMethods()).thenReturn(all);
	when(second.allowedMethods()).thenReturn(all);

	when(route.controllerMethod(request, "/clients/add")).thenReturn(method);
	when(route.getPriority()).thenReturn(Path.HIGHEST);
	when(second.getPriority()).thenReturn(Path.LOWEST);

	router.add(route);
	router.add(second);

	ControllerMethod found = router.parse("/clients/add", HttpMethod.POST, request);
	assertThat(found, is(equalTo(method)));
}
 
Example #5
Source File: DefaultRefererResultTest.java    From vraptor4 with Apache License 2.0 6 votes vote down vote up
@Test
public void whenRefererMatchesAControllerShouldRedirectToIt() throws Exception {
	LogicResult logic = mock(LogicResult.class);
	RefererController controller = mock(RefererController.class);

	Method index = RefererController.class.getMethod("index");
	ControllerMethod method = DefaultControllerMethod.instanceFor(RefererController.class, index);

	when(request.getHeader("Referer")).thenReturn("http://localhost:8080/vraptor/no-controller");
	when(request.getContextPath()).thenReturn("/vraptor");
	when(router.parse("/no-controller", HttpMethod.GET, request)).thenReturn(method);
	doReturn(logic).when(result).use(logic());
	when(logic.redirectTo(RefererController.class)).thenReturn(controller);

	refererResult.redirect();
	
	verify(logic).redirectTo(RefererController.class);
	verify(controller).index();
}
 
Example #6
Source File: PathAnnotationRoutesParserTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAcceptAResultWithPatchWebMethod() throws SecurityException, NoSuchMethodException {
	List<Route> routes = parser.rulesFor(new DefaultBeanClass(ClientsController.class));
	Route route = getRouteMatching(routes, "/clients/update");

	assertThat(route.allowedMethods(), is(EnumSet.of(HttpMethod.PATCH)));
}
 
Example #7
Source File: DefaultRouterTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void acceptsAnHttpMethodLimitedMappingRule() throws NoSuchMethodException {
	new Rules(router) {
		@Override
		public void routes() {
			routeFor("/clients/add").with(HttpMethod.POST).is(SomeController.class).add(null);
		}
	};
	assertThat(router.parse("/clients/add", HttpMethod.POST, request), is(VRaptorMatchers.controllerMethod(method(
			"add", Dog.class))));
}
 
Example #8
Source File: DefaultRouterTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void acceptsAnHttpMethodLimitedMappingRuleWithBothMethods() throws NoSuchMethodException {
	new Rules(router) {
		@Override
		public void routes() {
			routeFor("/clients/add").with(HttpMethod.POST).with(HttpMethod.GET).is(SomeController.class).add(null);
		}
	};

	assertThat(router.parse("/clients/add", HttpMethod.POST, request), is(VRaptorMatchers.controllerMethod(method(
			"add", Dog.class))));

	assertThat(router.parse("/clients/add", HttpMethod.GET, request), is(VRaptorMatchers.controllerMethod(method(
			"add", Dog.class))));
}
 
Example #9
Source File: DefaultRouterTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void usesAsteriskBothWays() throws NoSuchMethodException {
	registerRulesFor(MyController.class);
	final Method method = MyController.class.getMethod("starPath");
	String url = router.urlFor(MyController.class, method, new Object[] {});
	assertThat(router.parse(url, HttpMethod.POST, null).getMethod(), is(equalTo(method)));
}
 
Example #10
Source File: DefaultRouterTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCacheInvocationsAfterFirstCall() throws NoSuchMethodException {
	registerRulesFor(MyController.class);
	final Method method = MyController.class.getMethod("starPath");
	router.urlFor(MyController.class, method, new Object[] {});
	String url = router.urlFor(MyController.class, method, new Object[] {});
	assertThat(router.parse(url, HttpMethod.POST, null).getMethod(), is(equalTo(method)));
}
 
Example #11
Source File: PathAnnotationRoutesParserTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAcceptAResultWithOptionsWebMethod() throws SecurityException, NoSuchMethodException {
	List<Route> routes = parser.rulesFor(new DefaultBeanClass(ClientsController.class));
	Route route = getRouteMatching(routes, "/clients/options");

	assertThat(route.allowedMethods(), is(EnumSet.of(HttpMethod.OPTIONS)));
}
 
Example #12
Source File: DefaultRouterTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void canTranslateAInheritedControllerBothWays() throws NoSuchMethodException {
	registerRulesFor(MyController.class);
	registerRulesFor(InheritanceExample.class);
	final Method method = MyController.class.getMethod("notAnnotated");
	String url = router.urlFor(InheritanceExample.class, method, new Object[] {});
	assertThat(router.parse(url, HttpMethod.POST, null).getMethod(), is(equalTo(method)));
}
 
Example #13
Source File: DefaultRouterTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void canTranslateAnnotatedMethodBothWays() throws NoSuchMethodException {
	registerRulesFor(MyController.class);
	final Method method = MyController.class.getMethod("customizedPath");
	String url = router.urlFor(MyController.class, method, new Object[] {});
	assertThat(router.parse(url, HttpMethod.POST, null).getMethod(), is(equalTo(method)));
}
 
Example #14
Source File: FixedMethodStrategyTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	MockitoAnnotations.initMocks(this);
	list = DefaultControllerMethod.instanceFor(MyControl.class, method("list"));
	get = EnumSet.of(HttpMethod.GET);
	post = EnumSet.of(HttpMethod.POST);
}
 
Example #15
Source File: RequestHandlerObserverTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandle405() throws Exception {
	EnumSet<HttpMethod> allowedMethods = EnumSet.of(HttpMethod.GET);
	when(translator.translate(webRequest)).thenThrow(new MethodNotAllowedException(allowedMethods, POST.toString()));
	observer.handle(requestStarted);
	verify(methodNotAllowedHandler).deny(webRequest, webResponse, allowedMethods);
	verify(interceptorStack, never()).start();
}
 
Example #16
Source File: DefaultRouterTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void throwsExceptionIfMoreThanOneUriMatchesWithSamePriority() throws NoSuchMethodException {
	Route route = mock(Route.class);
	Route second = mock(Route.class, "second");
	when(route.getControllerMethod()).thenReturn(anyControllerMethod());
	when(second.getControllerMethod()).thenReturn(anyControllerMethod());

	when(route.canHandle("/clients/add")).thenReturn(true);
	when(second.canHandle("/clients/add")).thenReturn(true);

	EnumSet<HttpMethod> all = EnumSet.allOf(HttpMethod.class);

	when(route.allowedMethods()).thenReturn(all);
	when(second.allowedMethods()).thenReturn(all);

	when(route.getPriority()).thenReturn(Path.DEFAULT);
	when(second.getPriority()).thenReturn(Path.DEFAULT);

	router.add(route);
	router.add(second);

	try {
		router.parse("/clients/add", HttpMethod.POST, request);
		fail("IllegalStateException expected");
	} catch (IllegalStateException e) {
	}
}
 
Example #17
Source File: PathAnnotationRoutesParserTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotMatchIfAControllerHasTheWrongWebMethod() throws SecurityException {
	List<Route> routes = parser.rulesFor(new DefaultBeanClass(ClientsController.class));
	Route route = getRouteMatching(routes, "/clients/remove");

	assertThat(route.allowedMethods(), not(contains(HttpMethod.POST)));
}
 
Example #18
Source File: DefaultControllerTranslatorTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void canHandleUrlIfNonRootContextButPlainRequest() {
	when(request.getRequestURI()).thenReturn("/custom_context/");
	when(request.getContextPath()).thenReturn("/custom_context");
	when(request.getMethod()).thenReturn("GET");
	when(router.parse("/", HttpMethod.GET, webRequest)).thenReturn(method);

	ControllerMethod controller = translator.translate(webRequest);
	assertThat(controller, is(equalTo(method)));
}
 
Example #19
Source File: DefaultControllerTranslatorTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void canHandleUrlWithAppendedJSessionID() {
	when(request.getRequestURI()).thenReturn(
			"/custom_context/products/1;jsessionid=aslfasfaslkj22234lkjsdfaklsf",
			"/custom_context/products/1;JSESSIONID=aslfasfaslkj22234lkjsdfaklsf",
			"/custom_context/products/1;jsessionID=aslfasfaslkj22234lkjsdfaklsf");
	when(request.getContextPath()).thenReturn("/custom_context");
	when(request.getMethod()).thenReturn("GET");
	when(router.parse("/products/1", HttpMethod.GET, webRequest)).thenReturn(method);

	assertThat(translator.translate(webRequest), is(equalTo(method)));
	assertThat(translator.translate(webRequest), is(equalTo(method)));
	assertThat(translator.translate(webRequest), is(equalTo(method)));

}
 
Example #20
Source File: DefaultControllerTranslatorTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void canHandleComposedUrlIfNonRootContext() {
	when(request.getRequestURI()).thenReturn("/custom_context/products/1");
	when(request.getContextPath()).thenReturn("/custom_context");
	when(request.getMethod()).thenReturn("GET");
	when(router.parse("/products/1", HttpMethod.GET, webRequest)).thenReturn(method);

	ControllerMethod controller = translator.translate(webRequest);
	assertThat(controller, is(equalTo(method)));
}
 
Example #21
Source File: DefaultControllerTranslatorTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void canHandleComposedUrlIfPlainRootContext() {
	when(request.getRequestURI()).thenReturn("/products/1");
	when(request.getMethod()).thenReturn("GET");
	when(router.parse("/products/1", HttpMethod.GET, webRequest)).thenReturn(method);

	ControllerMethod controller = translator.translate(webRequest);
	assertThat(controller, is(equalTo(method)));
}
 
Example #22
Source File: DefaultControllerTranslatorTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void canHandleUrlIfPlainRootContext() {
	when(request.getRequestURI()).thenReturn("/");
	when(request.getMethod()).thenReturn("GET");
	when(router.parse("/", HttpMethod.GET, webRequest)).thenReturn(method);

	ControllerMethod controller = translator.translate(webRequest);
	assertThat(controller, is(equalTo(method)));
  	}
 
Example #23
Source File: DefaultControllerTranslatorTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void canHandleUrlIfNonRootContext() {
	when(request.getRequestURI()).thenReturn("/custom_context/url");
	when(request.getContextPath()).thenReturn("/custom_context");
	when(request.getMethod()).thenReturn("GET");
	when(router.parse("/url", HttpMethod.GET, webRequest)).thenReturn(method);

	ControllerMethod controller = translator.translate(webRequest);
	assertThat(controller, is(equalTo(method)));
}
 
Example #24
Source File: DefaultControllerTranslatorTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void canHandleUrlIfRootContext() {
	when(request.getRequestURI()).thenReturn("/url");
	when(request.getContextPath()).thenReturn("");
	when(request.getMethod()).thenReturn("GET");
	when(router.parse("/url", HttpMethod.GET, webRequest)).thenReturn(method);

	ControllerMethod controller = translator.translate(webRequest);
	assertThat(controller, is(equalTo(method)));

}
 
Example #25
Source File: DefaultControllerTranslatorTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldOverrideTheHttpMethodByUsingThe_methodParameter() {
	when(request.getRequestURI()).thenReturn("/url");
	when(request.getParameter("_method")).thenReturn("DELETE");
	when(request.getMethod()).thenReturn("POST");
	when(router.parse("/url", HttpMethod.DELETE, webRequest)).thenReturn(method);

	ControllerMethod controller = translator.translate(webRequest);
	assertThat(controller, is(equalTo(method)));
}
 
Example #26
Source File: DefaultControllerTranslatorTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldThrowExceptionWhenRequestANotKnownMethod() {
	exception.expect(MethodNotAllowedException.class);
	exception.expectMessage("Method COOK is not allowed for requested URI. Allowed Methods are [GET, POST]");
	
	when(request.getRequestURI()).thenReturn("/url");
	when(request.getMethod()).thenReturn("COOK");
	when(router.parse(anyString(), any(HttpMethod.class), any(MutableRequest.class))).thenReturn(method);
	when(router.allowedMethodsFor("/url")).thenReturn(EnumSet.of(HttpMethod.GET, HttpMethod.POST));

	translator.translate(webRequest);
}
 
Example #27
Source File: DefaultControllerTranslatorTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAcceptCaseInsensitiveGetRequestUsingThe_methodParameter() {
	when(request.getRequestURI()).thenReturn("/url");
	when(request.getParameter("_method")).thenReturn("gEt");
	when(request.getMethod()).thenReturn("POST");
	when(router.parse("/url", HttpMethod.GET, webRequest)).thenReturn(method);

	ControllerMethod controller = translator.translate(webRequest);
	assertThat(controller, is(equalTo(method)));
}
 
Example #28
Source File: DefaultControllerTranslatorTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAcceptCaseInsensitiveRequestMethods() {
	when(request.getRequestURI()).thenReturn("/url");
	when(request.getMethod()).thenReturn("pOsT");
	when(router.parse("/url", HttpMethod.POST,webRequest)).thenReturn(method);

	ControllerMethod controller = translator.translate(webRequest);

	assertThat(controller, is(equalTo(method)));
}
 
Example #29
Source File: DefaultControllerTranslatorTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void canHandleTheCorrectMethod() {

	when(request.getRequestURI()).thenReturn("/url");
	when(request.getMethod()).thenReturn("POST");
	when(router.parse("/url", HttpMethod.POST,webRequest)).thenReturn(method);

	ControllerMethod controller = translator.translate(webRequest);
	assertThat(controller, is(equalTo(method)));
}
 
Example #30
Source File: DefaultControllerTranslatorTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void handlesInclude() {

	when(request.getAttribute(INCLUDE_REQUEST_URI)).thenReturn("/url");
	when(request.getMethod()).thenReturn("POST");
	when(router.parse("/url", HttpMethod.POST, webRequest)).thenReturn(method);

	ControllerMethod controller = translator.translate(webRequest);
	assertThat(controller, is(sameInstance(method)));
}