Skip to content

Commit d5ceff9

Browse files
committed
Merge branch 'master' of https://github.com/Kerry-G/HTTP-Library
2 parents 474bffd + 94ef113 commit d5ceff9

3 files changed

Lines changed: 24 additions & 45 deletions

File tree

src/main/java/CLI/Httpc.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public Httpc(){
2929
jc.setProgramName("Httpc");
3030
}
3131

32-
public Optional<Response> interpret(){
32+
public Optional<Response> interpret(String ...args){
3333
try {
34+
jc.parse(args);
3435
switch (CommandType.fromString(jc.getParsedCommand())) {
3536
case GET:
3637
return Optional.of(commandGet.run());

src/main/java/Driver.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,15 @@ public static void main(String[] args) {
1818
jc.usage();
1919
return;
2020
}
21-
try{
22-
jc.parse(args);
23-
httpc.interpret().ifPresent(response -> {
24-
Logger.debug(
25-
response.getVersion() + Constants.SPACE + response.getStatus() + Constants.SPACE + response.getPhrase());
26-
for (Map.Entry<String, String> header : response.getHeaders().entrySet()) {
27-
Logger.debug(header.getKey() + ": " + header.getValue());
28-
}
29-
Logger.println(response.getBody());
30-
});
31-
} catch (Exception e){
32-
Logger.println(e.getMessage());
33-
}
21+
22+
httpc.interpret().ifPresent(response -> {
23+
Logger.debug(
24+
response.getVersion() + Constants.SPACE + response.getStatus() + Constants.SPACE + response.getPhrase());
25+
for (Map.Entry<String, String> header : response.getHeaders().entrySet()) {
26+
Logger.debug(header.getKey() + ": " + header.getValue());
27+
}
28+
Logger.println(response.getBody());
29+
});
3430

3531

3632
}

src/test/HttpcTests.java

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ void GetStatus200(){
2121

2222
String[] argv = new String[]{"GET", "http://www.httpbin.org/"};
2323

24-
httpcJc.parse(argv);
25-
26-
final Response response = httpc.interpret().orElse(null);
24+
final Response response = httpc.interpret(argv).orElse(null);
2725
assertNotNull(response);
2826
assertEquals(200, response.getStatus());
2927
}
@@ -38,9 +36,8 @@ void ErrorHandlingUrlMalformatted(){
3836

3937
String[] argv = new String[]{"GET", "sasd..ww.httpbin.asdasd...org/"};
4038

41-
httpcJc.parse(argv);
4239

43-
final Response response = httpc.interpret().orElse(null);
40+
final Response response = httpc.interpret(argv).orElse(null);
4441
assertNull(response);
4542
//assertTrue(testablePrintStream.getInternalListString().stream().anyMatch(s -> s.equals("[ERROR]: No such URL is known")));
4643

@@ -53,9 +50,7 @@ void GetStatus418(){
5350

5451
String[] argv = new String[]{"GET", "http://www.httpbin.org/status/418"};
5552

56-
httpcJc.parse(argv);
57-
58-
final Response response = httpc.interpret().orElse(null);
53+
final Response response = httpc.interpret(argv).orElse(null);
5954
assertNotNull(response);
6055
assertEquals(418, response.getStatus());
6156
assertEquals("I'M A TEAPOT", response.getPhrase());
@@ -68,9 +63,8 @@ void PostStatus200(){
6863

6964
String[] argv = new String[]{"POST", "http://www.httpbin.org/status/204"};
7065

71-
httpcJc.parse(argv);
7266

73-
final Response response = httpc.interpret().orElse(null);
67+
final Response response = httpc.interpret(argv).orElse(null);
7468
assertNotNull(response);
7569
assertEquals(204, response.getStatus());
7670
}
@@ -84,9 +78,8 @@ void PostDataQuery(){
8478

8579
String[] argv = new String[]{"POST", "http://www.httpbin.org/anything" + query};
8680

87-
httpcJc.parse(argv);
8881

89-
final Response response = httpc.interpret().orElse(null);
82+
final Response response = httpc.interpret(argv).orElse(null);
9083
assertNotNull(response);
9184
System.out.println(response.getBody());
9285
assertTrue(response.getBody().contains(query));
@@ -101,9 +94,7 @@ void PostDataInline(){
10194

10295
String[] argv = new String[]{"POST", "http://www.httpbin.org/anything", "-d", data};
10396

104-
httpcJc.parse(argv);
105-
106-
final Response response = httpc.interpret().orElse(null);
97+
final Response response = httpc.interpret(argv).orElse(null);
10798
assertNotNull(response);
10899
System.out.println(response.getBody());
109100
assertTrue(response.getBody().contains(data));
@@ -116,9 +107,7 @@ void PostDataWithFileUsingRelativePath(){
116107

117108
String[] argv = new String[]{"POST", "http://www.httpbin.org/anything", "-f", "src/test/data.txt"};
118109

119-
httpcJc.parse(argv);
120-
121-
final Response response = httpc.interpret().orElse(null);
110+
final Response response = httpc.interpret(argv).orElse(null);
122111
assertNotNull(response);
123112
System.out.println(response.getBody());
124113
assertEquals(200, response.getStatus());
@@ -137,9 +126,7 @@ void PostDataWithFileAndData(){
137126

138127
String[] argv = new String[]{"POST", "http://www.httpbin.org/anything", "-d", " data", "-f", "file" };
139128

140-
httpcJc.parse(argv);
141-
142-
final Response response = httpc.interpret().orElse(null);
129+
final Response response = httpc.interpret(argv).orElse(null);
143130
assertNull(response);
144131

145132
}
@@ -150,9 +137,8 @@ void GetRedirectionUnderMaxTries(){
150137
final JCommander httpcJc = httpc.getJc();
151138

152139
String[] argv = new String[]{"GET", "-v", "http://httpbin.org/absolute-redirect/" + Integer.toString(Request.MAXTRIES-1)};
153-
httpcJc.parse(argv);
154140

155-
final Response response = httpc.interpret().orElse(null);
141+
final Response response = httpc.interpret(argv).orElse(null);
156142
Logger.println(response.getHeaders().toString());
157143
assertNotNull(response);
158144
assertEquals(200, response.getStatus());
@@ -164,9 +150,8 @@ void GetRedirectionOverMaxTries(){
164150
final JCommander httpcJc = httpc.getJc();
165151

166152
String[] argv = new String[]{"GET", "-v", "http://httpbin.org/absolute-redirect/" + Integer.toString(Request.MAXTRIES+1)};
167-
httpcJc.parse(argv);
168153

169-
final Response response = httpc.interpret().orElse(null);
154+
final Response response = httpc.interpret(argv).orElse(null);
170155
Logger.println(response.getHeaders().toString());
171156
assertNotNull(response);
172157
assertEquals(302, response.getStatus());
@@ -181,8 +166,7 @@ void TestHelpCommand(){
181166
final JCommander httpcJc = httpc.getJc();
182167

183168
String[] argv = new String[]{"help"};
184-
httpcJc.parse(argv);
185-
httpc.interpret().orElse(null);
169+
httpc.interpret(argv).orElse(null);
186170

187171
assertTrue(stream.getInternalListString().size() > 0);
188172
assertTrue(stream.getInternalListString().get(0).contains("httpc is a curl-like application but supports HTTP protocol only."));
@@ -197,8 +181,7 @@ void TestHelpGet(){
197181
final JCommander httpcJc = httpc.getJc();
198182

199183
String[] argv = new String[]{"help", "get"};
200-
httpcJc.parse(argv);
201-
httpc.interpret().orElse(null);
184+
httpc.interpret(argv).orElse(null);
202185

203186
assertTrue(stream.getInternalListString().size() > 0);
204187
assertTrue(stream.getInternalListString().get(0).contains("usage: httpc get [-v] [-h key:value] URL"));
@@ -213,8 +196,7 @@ void TestHelpPost(){
213196
final JCommander httpcJc = httpc.getJc();
214197

215198
String[] argv = new String[]{"help", "post"};
216-
httpcJc.parse(argv);
217-
httpc.interpret().orElse(null);
199+
httpc.interpret(argv).orElse(null);
218200

219201
assertTrue(stream.getInternalListString().size() > 0);
220202
assertTrue(stream.getInternalListString().get(0).contains("usage: httpc post [-v] [-h key:value] [-d inline-data] [-f file] URL"));

0 commit comments

Comments
 (0)