*
* @throws Exception
*/
@Test
public void testConfidentialAuthCodeFlow() throws Exception {
FakeHttpServletRequest req = new FakeHttpServletRequest(
"http://localhost:8080", "/oauth2", "client_id=" + CONF_CLIENT_ID
+ "&client_secret=" + CONF_CLIENT_SECRET
+ "&response_type=code&redirect_uri="
+ URLEncoder.encode(REDIRECT_URI, "UTF-8"));
req.setMethod("GET");
req.setServletPath("/oauth2");
req.setPathInfo("/authorize");
HttpServletResponse resp = mock(HttpServletResponse.class);
Capture<String> redirectURI = new Capture<String>();
resp.setHeader(EasyMock.eq("Location"), EasyMock.capture(redirectURI));
resp.setStatus(EasyMock.eq(HttpServletResponse.SC_FOUND));
MockServletOutputStream outputStream = new MockServletOutputStream();
EasyMock.expect(resp.getOutputStream()).andReturn(outputStream).anyTimes();
PrintWriter writer = new PrintWriter(outputStream);
EasyMock.expect(resp.getWriter()).andReturn(writer).anyTimes();
replay();
servlet.service(req, resp);
writer.flush();
String response = new String(outputStream.getBuffer(), "UTF-8");
assertTrue(response == null || response.equals(""));
verify();
assertTrue(redirectURI.getValue().startsWith(REDIRECT_URI + "?code="));
String code = redirectURI.getValue().substring(
redirectURI.getValue().indexOf("=") + 1);
UUID id = UUID.fromString(code);
assertTrue(id != null);
reset();
req = new FakeHttpServletRequest("http://localhost:8080", "/oauth2",
"client_id=" + CONF_CLIENT_ID
+ "&grant_type=authorization_code&redirect_uri="
+ URLEncoder.encode(REDIRECT_URI, "UTF-8") + "&code=" + code
+ "&client_secret=" + CONF_CLIENT_SECRET);
req.setMethod("GET");
req.setServletPath("/oauth2");
req.setPathInfo("/access_token");
resp = mock(HttpServletResponse.class);
resp.setStatus(HttpServletResponse.SC_OK);
outputStream = new MockServletOutputStream();
EasyMock.expect(resp.getOutputStream()).andReturn(outputStream).anyTimes();
writer = new PrintWriter(outputStream);