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