Skip to content

Commit f6e2b3a

Browse files
committed
Dry up tests
1 parent edf854f commit f6e2b3a

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

src/main/java/httpserver/Authorizer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
public class Authorizer {
66
public boolean authorize(Request request) {
77
if (request.hasHeader("Authorization")) {
8-
String[] credentials = decodeBasicAuthHeader(request.getHeaderValue("Authorization"));
8+
String authorizationHeader = request.getHeaderValue("Authorization");
9+
String[] credentials = decodeBasicAuthHeader(authorizationHeader);
10+
911
if (validateCredentials(credentials)) {
1012
return true;
1113
}

src/test/java/httpserver/LoggerTest.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import httpserver.file.FileOperator;
44
import org.junit.Test;
55

6+
import java.io.IOException;
67
import java.nio.file.Path;
78
import java.nio.file.Paths;
89
import java.nio.file.StandardOpenOption;
@@ -14,12 +15,16 @@
1415
import static org.junit.Assert.*;
1516

1617
public class LoggerTest {
17-
@Test
18-
public void createsLogFileOnConstructDoesntOverwrite() throws Exception {
19-
Path logPath = Paths.get(tempDir().toString(), "logs");
2018

21-
new Logger(logPath, new FileOperator());
19+
private final Path logPath;
20+
private final Logger logger;
2221

22+
public LoggerTest() throws IOException {
23+
logPath = Paths.get(tempDir().toString(), "logs");
24+
logger = new Logger(logPath, new FileOperator());
25+
}
26+
@Test
27+
public void createsLogFileOnConstructDoesntOverwrite() throws Exception {
2328
assertTrue(exists(logPath));
2429

2530
write(logPath, "fake log line\r\n".getBytes(),
@@ -31,24 +36,12 @@ public void createsLogFileOnConstructDoesntOverwrite() throws Exception {
3136
}
3237

3338
@Test
34-
public void onLogCallsFileOperatorAppendWithPathAndPayload() throws Exception {
35-
Path logPath = Paths.get(tempDir().toString(), "logs");
36-
Logger logger = new Logger(logPath, new FileOperator());
37-
39+
public void writesToLogAndReadsFromLog() throws Exception {
3840
logger.log("POST example");
3941
logger.log("HEAD example");
4042

41-
assertEquals("POST example\r\nHEAD example\r\n", new String(readAllBytes(logPath)));
42-
}
43-
44-
@Test
45-
public void readsLog() throws Exception {
46-
Path logPath = Paths.get(tempDir().toString(), "logs");
47-
Logger logger = new Logger(logPath, new FileOperator());
48-
49-
logger.log("POST example");
50-
logger.log("HEAD example");
43+
String log = new String(logger.readLog());
5144

52-
assertEquals("POST example\r\nHEAD example\r\n", new String(logger.readLog()));
45+
assertEquals("POST example\r\nHEAD example\r\n", log);
5346
}
5447
}

0 commit comments

Comments
 (0)