Skip to content

Commit 0d26bc9

Browse files
committed
Changes mentioned from pull request. Added tests for the HTTP basic auth class.
1 parent fc3504b commit 0d26bc9

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

gen/src/main/java/com/softlayer/api/gen/MetaConverter.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,13 @@ public String getPackageName(String typeName) {
7272
typeName = typeName.substring(10);
7373
}
7474
String[] pieces = typeName.split("_");
75-
Arrays.stream(pieces)
76-
.limit(pieces.length - 1) // Skip the last, it's the class name
77-
.map(String::toLowerCase)
78-
.map(piece -> {
79-
String replacement = keywordReplacements.get(piece);
80-
return replacement != null ? replacement : piece;
81-
})
82-
.forEach(piece -> pkg.append('.').append(getValidJavaIdentifier(piece)));
75+
// Skip the last one, it's the class name
76+
for (int i = 0; i < pieces.length - 1; i++) {
77+
String piece = pieces[i].toLowerCase();
78+
String replacement = keywordReplacements.get(piece);
79+
piece = replacement != null ? replacement : piece;
80+
pkg.append('.').append(getValidJavaIdentifier(piece));
81+
}
8382
return pkg.toString();
8483
}
8584

src/main/java/com/softlayer/api/http/BuiltInHttpClientFactory.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,10 @@ public HttpResponse invokeSync(Callable<?> setupBody) {
139139
// fairly fast and safe.
140140
openConnection();
141141
if (credentials != null) {
142-
try {
143-
connection.addRequestProperty(
144-
"Authorization",
145-
credentials.getHeader()
146-
);
147-
} catch (UnsupportedEncodingException e) {
148-
throw new RuntimeException(e);
149-
}
142+
connection.addRequestProperty(
143+
"Authorization",
144+
credentials.getHeader()
145+
);
150146
}
151147
for (Map.Entry<String, List<String>> headerEntry : headers.entrySet()) {
152148
for (String headerValue : headerEntry.getValue()) {

src/main/java/com/softlayer/api/http/HttpBasicAuthCredentials.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.softlayer.api.http;
22

3-
import java.io.UnsupportedEncodingException;
43
import java.util.Base64;
54

65
/** HTTP basic authorization support for username and API key */
@@ -19,16 +18,10 @@ public HttpBasicAuthCredentials(String username, String apiKey) {
1918
* for use in an HTTP Authorization header.
2019
*
2120
* @return String
22-
* @throws UnsupportedEncodingException If encoding with UTF-8 fails.
2321
*/
24-
public String getHeader() throws UnsupportedEncodingException
22+
public String getHeader()
2523
{
2624
String authPair = username + ':' + apiKey;
27-
String result = "Basic ";
28-
result += new String(
29-
Base64.getEncoder().encode(authPair.getBytes("UTF-8")),
30-
"UTF-8"
31-
);
32-
return result;
25+
return "Basic " + Base64.getEncoder().encodeToString(authPair.getBytes());
3326
}
3427
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.softlayer.api.http;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class HttpBasicAuthCredentialsTest {
8+
9+
@Test
10+
public void testConstructor()
11+
{
12+
HttpBasicAuthCredentials authCredentials = new HttpBasicAuthCredentials("username", "apiKey");
13+
assertEquals("username", authCredentials.username);
14+
assertEquals("apiKey", authCredentials.apiKey);
15+
}
16+
17+
@Test
18+
public void testGetHeader()
19+
{
20+
HttpBasicAuthCredentials authCredentials = new HttpBasicAuthCredentials("username", "apiKey");
21+
assertEquals("Basic dXNlcm5hbWU6YXBpS2V5", authCredentials.getHeader());
22+
}
23+
}

0 commit comments

Comments
 (0)