Java Code Examples for org.springframework.mock.web.MockHttpServletRequest#setContentType()

The following examples show how to use org.springframework.mock.web.MockHttpServletRequest#setContentType() . 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: JsonRpcServerTest.java    From jsonrpc4j with MIT License 7 votes vote down vote up
@Test
public void test_contentType() throws Exception {
	EasyMock.expect(mockService.testMethod("Whir?inaki")).andReturn("For?est");
	EasyMock.replay(mockService);

	MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test-post");
	MockHttpServletResponse response = new MockHttpServletResponse();

	request.setContentType("application/json");
	request.setContent("{\"jsonrpc\":\"2.0\",\"id\":123,\"method\":\"testMethod\",\"params\":[\"Whir?inaki\"]}".getBytes(StandardCharsets.UTF_8));

	jsonRpcServer.setContentType("flip/flop");

	jsonRpcServer.handle(request, response);

	assertTrue("flip/flop".equals(response.getContentType()));
	checkSuccessfulResponse(response);
}
 
Example 2
Source File: KatharsisFilterTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnacceptableRequestContentType() throws Exception {
	MockFilterChain filterChain = new MockFilterChain();

	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath(null);
	request.setPathInfo(null);
	request.setRequestURI("/api/tasks/");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	request.addHeader("Accept", "application/xml");

	MockHttpServletResponse response = new MockHttpServletResponse();

	katharsisFilter.doFilter(request, response, filterChain);

	assertEquals(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getStatus());
	String responseContent = response.getContentAsString();
	assertTrue(responseContent == null || "".equals(responseContent.trim()));
}
 
Example 3
Source File: KatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testKatharsisInclude() throws Exception {

	Node root = new Node(1L, null, null);
	Node child1 = new Node(2L, root, Collections.<Node>emptySet());
	Node child2 = new Node(3L, root, Collections.<Node>emptySet());
	root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));
	nodeRepository.save(root);
	nodeRepository.save(child1);
	nodeRepository.save(child2);
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setServletPath("/api");
	request.setPathInfo("/nodes/1");
	request.setRequestURI("/api/nodes/1");
	request.setQueryString("include[nodes]=parent");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	Map<String, String> params = new HashMap<>();
	params.put("include[nodes]", "children");
	request.setParameters(params);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);
	String responseContent = response.getContentAsString();
	assertTopLevelNodesCorrectWithChildren(responseContent);
}
 
Example 4
Source File: KatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void onSimpleResourceGetShouldReturnOneResource() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath("/api");
	request.setPathInfo("/tasks/1");
	request.setRequestURI("/api/tasks/1");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	request.addHeader("Accept", "*/*");

	MockHttpServletResponse response = new MockHttpServletResponse();

	katharsisServlet.service(request, response);

	String responseContent = response.getContentAsString();

	log.debug("responseContent: {}", responseContent);
	assertNotNull(responseContent);

	assertJsonPartEquals("tasks", responseContent, "data.type");
	assertJsonPartEquals("\"1\"", responseContent, "data.id");
	assertJsonPartEquals(SOME_TASK_ATTRIBUTES, responseContent, "data.attributes");
	assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data.links");
	assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data.relationships.project.links");
}
 
Example 5
Source File: UploadServletTest.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private MockHttpServletRequest getMultipartRequest( ) throws Exception
{
    MockHttpServletRequest request = new MockHttpServletRequest( );
    byte [ ] fileContent = new byte [ ] {
            1, 2, 3
    };
    Part [ ] parts = new Part [ ] {
        new FilePart( "file1", new ByteArrayPartSource( "file1", fileContent ) )
    };
    MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity( parts, new PostMethod( ).getParams( ) );
    // Serialize request body
    ByteArrayOutputStream requestContent = new ByteArrayOutputStream( );
    multipartRequestEntity.writeRequest( requestContent );
    // Set request body to HTTP servlet request
    request.setContent( requestContent.toByteArray( ) );
    // Set content type to HTTP servlet request (important, includes Mime boundary string)
    request.setContentType( multipartRequestEntity.getContentType( ) );
    request.setMethod( "POST" );
    return request;
}
 
Example 6
Source File: JsonRpcServerTest.java    From jsonrpc4j with MIT License 6 votes vote down vote up
@Test
public void testGzipResponse() throws IOException {
	MockHttpServletRequest request = new MockHttpServletRequest("POST", "/test-post");
	request.addHeader(ACCEPT_ENCODING, "gzip");
	request.setContentType("application/json");
	request.setContent("{\"jsonrpc\":\"2.0\",\"id\":123,\"method\":\"testMethod\",\"params\":[\"Whir?inaki\"]}".getBytes(StandardCharsets.UTF_8));

	MockHttpServletResponse response = new MockHttpServletResponse();

	jsonRpcServer = new JsonRpcServer(Util.mapper, mockService, ServiceInterface.class, true);
	jsonRpcServer.handle(request, response);

	byte[] compressed = response.getContentAsByteArray();
	String sb = getCompressedResponseContent(compressed);

	Assert.assertEquals(sb, "{\"jsonrpc\":\"2.0\",\"id\":123,\"result\":null}");
	Assert.assertEquals("gzip", response.getHeader(CONTENT_ENCODING));
}
 
Example 7
Source File: SampleKatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void onSimpleCollectionGetShouldReturnCollectionOfResources() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath("/api");
	request.setPathInfo("/tasks/");
	request.setRequestURI("/api/tasks/");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	request.addHeader("Accept", "*/*");

	MockHttpServletResponse response = new MockHttpServletResponse();

	katharsisServlet.service(request, response);

	String responseContent = response.getContentAsString();

	log.debug("responseContent: {}", responseContent);
	assertNotNull(responseContent);

	assertJsonPartEquals("tasks", responseContent, "data[0].type");
	assertJsonPartEquals("\"1\"", responseContent, "data[0].id");
	assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data[0].attributes");
	assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data[0].links");
	assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data[0].relationships.project.links");
}
 
Example 8
Source File: SampleKatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testKatharsisMatchingException() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath("/api");
	request.setPathInfo("/tasks-matching-exception");
	request.setRequestURI("/api/matching-exception");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);

	String responseContent = response.getContentAsString();
	assertTrue("Response should not be returned for non matching resource type", StringUtils.isBlank(responseContent));
	assertEquals(404, response.getStatus());

}
 
Example 9
Source File: KatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnacceptableRequestContentType() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath("/api");
	request.setPathInfo("/tasks");
	request.setRequestURI("/api/tasks");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	request.addHeader("Accept", "application/xml");
	request.addParameter("filter[Task][name]", "John");
	request.setQueryString(URLEncoder.encode("filter[Task][name]", StandardCharsets.UTF_8.name()) + "=John");

	MockHttpServletResponse response = new MockHttpServletResponse();

	katharsisServlet.service(request, response);

	assertEquals(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getStatus());
	String responseContent = response.getContentAsString();
	assertTrue(responseContent == null || "".equals(responseContent.trim()));
}
 
Example 10
Source File: KatharsisServletTest.java    From katharsis-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void testKatharsisMatchingException() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setContextPath("");
	request.setServletPath("/api");
	request.setPathInfo("/tasks-matching-exception");
	request.setRequestURI("/api/matching-exception");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);

	String responseContent = response.getContentAsString();
	assertTrue("Response should not be returned for non matching resource type", StringUtils.isBlank(responseContent));
	assertEquals(404, response.getStatus());

}
 
Example 11
Source File: ServletNettyChannelHandler.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
void copyHttpHeaders(FullHttpRequest fullHttpReq, MockHttpServletRequest servletRequest){
 HttpHeaders headers = fullHttpReq.headers();
       for (String name : headers.names()) {
      	 servletRequest.addHeader(name, headers.get(name));
       }
       servletRequest.setContentType(headers.get(HttpHeaders.Names.CONTENT_TYPE));
}
 
Example 12
Source File: LoggingFilterTest.java    From servlet-logging-filter with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setUp() {

	httpServletRequest = new MockHttpServletRequest("GET", "http://localhost:8080/test");
	httpServletRequest.addHeader("Accept", "application/json");
	httpServletRequest.addParameter("param1", "1000");
	httpServletRequest.setContent("Test request body".getBytes());
	httpServletRequest.setContentType(MediaType.TEXT_PLAIN_VALUE);

	httpServletResponse = new MockHttpServletResponse();
	httpServletResponse.setContentType(MediaType.TEXT_PLAIN_VALUE);

	filterChain = new MockFilterChain(new HttpRequestHandlerServlet(), new TestFilter());
}
 
Example 13
Source File: SampleKatharsisFilterTest.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void onCollectionRequestWithParamsGetShouldReturnCollection() throws Exception {
    MockFilterChain filterChain = new MockFilterChain();

    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath(null);
    request.setPathInfo(null);
    request.setRequestURI("/api/tasks");
    request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
    request.addHeader("Accept", "*/*");
    request.addParameter("filter[Task][name]", "John");
    request.setQueryString(URLEncoder.encode("filter[Task][name]", StandardCharsets.UTF_8.name()) + "=John");

    MockHttpServletResponse response = new MockHttpServletResponse();

    katharsisFilter.doFilter(request, response, filterChain);

    String responseContent = response.getContentAsString();

    log.debug("responseContent: {}", responseContent);
    assertNotNull(responseContent);

    assertJsonPartEquals("tasks", responseContent, "data[0].type");
    assertJsonPartEquals("\"1\"", responseContent, "data[0].id");
    assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data[0].attributes");
    assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data[0].links");
    assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data[0].relationships.project.links");
}
 
Example 14
Source File: SampleKatharsisFilterTest.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void onSimpleResourceGetShouldReturnOneResource() throws Exception {
    MockFilterChain filterChain = new MockFilterChain();

    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath(null);
    request.setPathInfo(null);
    request.setRequestURI("/api/tasks/1");
    request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
    request.addHeader("Accept", "*/*");

    MockHttpServletResponse response = new MockHttpServletResponse();

    katharsisFilter.doFilter(request, response, filterChain);

    String responseContent = response.getContentAsString();

    log.debug("responseContent: {}", responseContent);
    assertNotNull(responseContent);

    assertJsonPartEquals("tasks", responseContent, "data.type");
    assertJsonPartEquals("\"1\"", responseContent, "data.id");
    assertJsonPartEquals(SOME_TASK_ATTRIBUTES, responseContent, "data.attributes");
    assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data.links");
    assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data.relationships.project.links");
}
 
Example 15
Source File: SampleKatharsisServletTest.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testKatharsisIncludeNestedWithDefault() throws Exception {
	Node root = new Node(1L, null, null);
	Locale engLocale = new Locale(1L, java.util.Locale.ENGLISH);
	Node child1 = new Node(2L, root, Collections.<Node>emptySet());
	NodeComment child1Comment = new NodeComment(1L, "Child 1", child1, engLocale);
	child1.setNodeComments(new LinkedHashSet<>(Collections.singleton(child1Comment)));
	Node child2 = new Node(3L, root, Collections.<Node>emptySet(), Collections.<NodeComment>emptySet());
	root.setChildren(new LinkedHashSet<>(Arrays.asList(child1, child2)));
	nodeRepository.save(root);
	nodeRepository.save(child1);
	nodeRepository.save(child2);

	MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
	request.setMethod("GET");
	request.setServletPath("/api");
	request.setPathInfo("/nodes/1");
	request.setRequestURI("/api/nodes/1");
	request.setQueryString("include[nodes]=children.nodeComments");
	request.setContentType(JsonApiMediaType.APPLICATION_JSON_API);
	Map<String, String> params = new HashMap<>();
	params.put("include[nodes]", "children.nodeComments.langLocale");
	request.setParameters(params);
	request.addHeader("Accept", "*/*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	katharsisServlet.service(request, response);
	String responseContent = response.getContentAsString();
	assertTopLevelNodesCorrectWithChildren(responseContent);
}
 
Example 16
Source File: CrnkFilterTest.java    From crnk-framework with Apache License 2.0 5 votes vote down vote up
@Test
public void onSimpleCollectionGetShouldReturnCollectionOfResources() throws Exception {
    MockFilterChain filterChain = new MockFilterChain();

    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath("/api");
    request.setPathInfo(null);
    request.setRequestURI("/api/tasks/");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    request.addHeader("Accept", "*/*");

    MockHttpServletResponse response = new MockHttpServletResponse();

    filter.doFilter(request, response, filterChain);

    Assert.assertEquals(200, response.getStatus());

    String responseContent = response.getContentAsString();

    log.debug("responseContent: {}", responseContent);
    assertNotNull(responseContent);

    assertJsonPartEquals("tasks", responseContent, "data[0].type");
    assertJsonPartEquals("\"1\"", responseContent, "data[0].id");
    assertJsonPartEquals(FIRST_TASK_ATTRIBUTES, responseContent, "data[0].attributes");
    assertJsonPartEquals(FIRST_TASK_LINKS, responseContent, "data[0].links");
    assertJsonPartEquals(PROJECT1_RELATIONSHIP_LINKS, responseContent, "data[0].relationships.project.links");
}
 
Example 17
Source File: MultipartConfigTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldDoGetRequestAndReturnFalse() {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/api/v1/");
    request.setContentType("multipart/");
    MultipartConfig multipartConfig = new MultipartConfig();
    assertFalse(multipartConfig.multipartResolver().isMultipart(request));
}
 
Example 18
Source File: RepositoryServletSecurityTest.java    From archiva with Apache License 2.0 4 votes vote down vote up
@Test
public void testPutWithValidUserWithWriteAccess()
    throws Exception
{
    assertTrue( Files.exists(repoRootInternal.getRoot()) );

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
    InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
    assertNotNull( "artifact.jar inputstream", is );

    servlet.setDavSessionProvider( davSessionProvider );

    ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
    archivaDavResourceFactory.setHttpAuth( httpAuth );
    archivaDavResourceFactory.setServletAuth( servletAuth );

    TestAuditListener listener = new TestAuditListener();
    archivaDavResourceFactory.addAuditListener( listener );
    servlet.setResourceFactory( archivaDavResourceFactory );

    AuthenticationResult result = new AuthenticationResult();

    EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
                                                       anyObject( HttpServletResponse.class ) ) ).andReturn(
        result );

    EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
                                                  anyObject( AuthenticationResult.class ) ) ).andReturn( true );

    User user = new SimpleUser();
    user.setUsername( "admin" );

    // ArchivaDavResourceFactory#isAuthorized()
    SecuritySession session = new DefaultSecuritySession();

    EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
                                                       anyObject( HttpServletResponse.class ) ) ).andReturn(
        result );

    EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession() ) ).andReturn( session );

    EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( user );

    EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
        true );

    EasyMock.expect(
        servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
                                  eq( ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ) ).andReturn( true );

    httpAuthControl.replay();
    servletAuthControl.replay();

    mockHttpServletRequest.addHeader( "User-Agent", "foo" );
    mockHttpServletRequest.setMethod( "PUT" );
    mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
    mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
    mockHttpServletRequest.setContentType( "application/octet-stream" );

    MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

    servlet.service( mockHttpServletRequest, mockHttpServletResponse );

    httpAuthControl.verify();
    servletAuthControl.verify();

    assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() );

    assertEquals( "admin", listener.getEvents().get( 0 ).getUserId() );
}
 
Example 19
Source File: RepositoryServletSecurityTest.java    From archiva with Apache License 2.0 4 votes vote down vote up
@Test
public void testPutWithInvalidUserAndGuestHasNoWriteAccess()
    throws Exception
{
    
    InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
    assertNotNull( "artifact.jar inputstream", is );

    servlet.setDavSessionProvider( davSessionProvider );

    AuthenticationResult result = new AuthenticationResult();

    EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
                                                       anyObject( HttpServletResponse.class ) ) ).andReturn(
        result );

    servletAuth.isAuthenticated( EasyMock.anyObject( HttpServletRequest.class ),
                                 EasyMock.anyObject( AuthenticationResult.class ) );
    EasyMock.expectLastCall().andThrow( new AuthenticationException( "Authentication error" ) );

    servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );

    EasyMock.expectLastCall().andThrow( new UnauthorizedException( "'guest' has no write access to repository" ) );

    httpAuthControl.replay();
    servletAuthControl.replay();
    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    mockHttpServletRequest.addHeader( "User-Agent", "foo" );
    mockHttpServletRequest.setMethod( "PUT" );
    mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
    mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
    mockHttpServletRequest.setContentType( "application/octet-stream" );

    MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

    servlet.service( mockHttpServletRequest, mockHttpServletResponse );

    httpAuthControl.verify();
    servletAuthControl.verify();

    assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
}
 
Example 20
Source File: RepositoryServletSecurityTest.java    From archiva with Apache License 2.0 4 votes vote down vote up
@Test
public void testPutWithValidUserWithNoWriteAccess()
    throws Exception
{
    
    servlet.setDavSessionProvider( davSessionProvider );

    ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
    archivaDavResourceFactory.setHttpAuth( httpAuth );
    archivaDavResourceFactory.setServletAuth( servletAuth );
    servlet.setResourceFactory( archivaDavResourceFactory );

    AuthenticationResult result = new AuthenticationResult();

    EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
                                                       anyObject( HttpServletResponse.class ) ) ).andReturn(
        result );

    EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
                                                  anyObject( AuthenticationResult.class ) ) ).andReturn( true );

    // ArchivaDavResourceFactory#isAuthorized()
    SecuritySession session = new DefaultSecuritySession();

    EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
                                                       anyObject( HttpServletResponse.class ) ) ).andReturn(
        result );

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();

    EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession( true ) ) ).andReturn(
        session );

    EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( new SimpleUser() );

    EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
        true );

    EasyMock.expect(
        servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
                                  eq( ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ) ).andThrow(
        new UnauthorizedException( "User not authorized" ) );
    httpAuthControl.replay();
    servletAuthControl.replay();

    InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
    assertNotNull( "artifact.jar inputstream", is );

    mockHttpServletRequest.addHeader( "User-Agent", "foo" );
    mockHttpServletRequest.setMethod( "PUT" );
    mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
    mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
    mockHttpServletRequest.setContentType( "application/octet-stream" );

    MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

    servlet.service( mockHttpServletRequest, mockHttpServletResponse );

    httpAuthControl.verify();
    servletAuthControl.verify();

    assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
}