View Javadoc

1   /*
2    * $Id: EndpointURIBuilderTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.endpoint;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.endpoint.EndpointURI;
15  import org.mule.api.endpoint.EndpointURIBuilder;
16  import org.mule.api.endpoint.MalformedEndpointException;
17  import org.mule.tck.junit4.AbstractMuleTestCase;
18  
19  import java.net.URI;
20  import java.net.URISyntaxException;
21  
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  
26  public class EndpointURIBuilderTestCase extends AbstractMuleTestCase
27  {
28      private static final String PLAIN_USERNAME_URI = "test://user:secret@theHost:42/path?key=value#fragment";
29      private static final String EXPECTED_PLAIN_URI_STRING = "test://user:****@theHost:42/path?key=value#fragment";
30      
31      private static final String USERNAME_WITH_AT_SIGN_URI = "test://user%40host:secret@theHost:42/path?key=value#fragment";
32      private static final String EXPECTED_AT_SIGN_URI_STRING = "test://user%40host:****@theHost:42/path?key=value#fragment";
33      private MuleContext unusedMuleContext = null;
34      
35      // Test for MULE-2720
36      @Test
37      public void testGetPropertiesForURI() throws MalformedEndpointException, URISyntaxException
38      {
39          UrlEndpointURIBuilder endpointURIBuilder = new UrlEndpointURIBuilder();
40          endpointURIBuilder.build(new URI("ftp://test%25user:test@192.168.1.12:21"), unusedMuleContext);
41          assertEquals("test%user:test", endpointURIBuilder.userInfo);
42      }
43      
44      @Test
45      public void testUrlEndpointBuilderPasswordMasking() throws Exception
46      {
47          UrlEndpointURIBuilder builder = new UrlEndpointURIBuilder();
48          checkUriWithPlainUsername(builder);
49      }
50      
51      @Test
52      public void testUrlEndpointBuilderPasswordMaskingWithAtSign() throws Exception
53      {
54          UrlEndpointURIBuilder builder = new UrlEndpointURIBuilder();
55          checkUriWithUsernameContainingAtSign(builder);
56      }
57      
58      @Test
59      public void testUserInfoEndpointBuilderPasswordMasking() throws Exception
60      {
61          UserInfoEndpointURIBuilder builder = new UserInfoEndpointURIBuilder();
62          checkUriWithPlainUsername(builder);
63      }
64  
65      @Test
66      public void testUserInfoEndpointBuilderPasswordMaskingWithAtSign() throws Exception
67      {
68          UserInfoEndpointURIBuilder builder = new UserInfoEndpointURIBuilder();
69          checkUriWithUsernameContainingAtSign(builder);
70      }
71      
72      private void checkUriWithPlainUsername(EndpointURIBuilder builder) throws Exception
73      {
74          URI inputUri = new URI(PLAIN_USERNAME_URI);
75          EndpointURI uri = builder.build(inputUri, unusedMuleContext);
76          
77          assertEquals("user", uri.getUser());
78          assertUriParts(uri);
79          
80          // assert that the password is properly masked
81          assertEquals(EXPECTED_PLAIN_URI_STRING, uri.toString());
82      }
83  
84      private void checkUriWithUsernameContainingAtSign(EndpointURIBuilder builder) throws Exception
85      {
86          URI inputUri = new URI(USERNAME_WITH_AT_SIGN_URI);
87          EndpointURI uri = builder.build(inputUri, unusedMuleContext);
88          
89          assertEquals("user@host", uri.getUser());
90          assertUriParts(uri);
91          
92          // assert that the password is properly masked
93          assertEquals(EXPECTED_AT_SIGN_URI_STRING, uri.toString());
94      }
95  
96      private void assertUriParts(EndpointURI uri)
97      {
98          // assert that the individual parts of the URI get preserved
99          assertEquals("secret", uri.getPassword());
100         assertEquals("theHost", uri.getHost());
101         assertEquals(42, uri.getPort());
102         assertEquals("/path", uri.getPath());
103         assertEquals("key=value", uri.getQuery());
104     }
105 }