Skip to content

Commit 3dea9dd

Browse files
author
a.pomosov
committed
Lecture04 slides and practice
1 parent a517301 commit 3dea9dd

3 files changed

Lines changed: 95 additions & 1 deletion

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.github.javaasasecondlanguage.chat.client;
2+
3+
import okhttp3.*;
4+
5+
import java.io.IOException;
6+
7+
8+
public class ChatClient {
9+
private static final OkHttpClient client = new OkHttpClient();
10+
private static final String PROTOCOL = "http://";
11+
private static final String HOST = "localhost";
12+
private static final String PORT = ":8080";
13+
14+
//POST host:port/chat/login?name=my_name
15+
public static Response login(String name) throws IOException {
16+
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
17+
Request request = new Request.Builder()
18+
.post(RequestBody.create(mediaType, ""))
19+
.url(PROTOCOL + HOST + PORT + "/chat/login?name=" + name)
20+
.build();
21+
22+
return client.newCall(request).execute();
23+
}
24+
25+
//GET host:port/chat/chat
26+
public static Response viewChat() throws IOException {
27+
Request request = new Request.Builder()
28+
.get()
29+
.url(PROTOCOL + HOST + PORT + "/chat/chat")
30+
.addHeader("host", HOST + PORT)
31+
.build();
32+
return client.newCall(request).execute();
33+
}
34+
35+
//POST host:port/chat/say?name=my_name
36+
//Body: "msg='my_message'"
37+
public static Response say(String name, String msg) throws IOException {
38+
throw new UnsupportedOperationException();
39+
}
40+
41+
//GET host:port/chat/online
42+
public static Response viewOnline() throws IOException {
43+
throw new UnsupportedOperationException();
44+
}
45+
}

chat/src/main/java/io/github/javaasasecondlanguage/chat/ChatController.java renamed to chat/src/main/java/io/github/javaasasecondlanguage/chat/server/ChatController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.javaasasecondlanguage.chat;
1+
package io.github.javaasasecondlanguage.chat.server;
22

33
import org.springframework.http.HttpStatus;
44
import org.springframework.http.MediaType;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.github.javaasasecondlanguage.chat;
2+
3+
import io.github.javaasasecondlanguage.chat.client.ChatClient;
4+
import okhttp3.Response;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.io.IOException;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
11+
12+
public class ChatClientTest {
13+
private static String MY_NAME_IN_CHAT = "I_CAN_COPY_PASTE";
14+
private static String MY_MESSAGE_TO_CHAT = "SOMEONE_KILL_ME";
15+
16+
@Test
17+
public void login() throws IOException {
18+
Response response = ChatClient.login(MY_NAME_IN_CHAT);
19+
System.out.println("[" + response + "]");
20+
String body = response.body().string();
21+
System.out.println();
22+
assertTrue(response.code() == 200 || body.equals("Already logged in:("));
23+
}
24+
25+
@Test
26+
public void viewChat() throws IOException {
27+
Response response = ChatClient.viewChat();
28+
System.out.println("[" + response + "]");
29+
System.out.println(response.body().string());
30+
assertEquals(200, response.code());
31+
}
32+
33+
34+
@Test
35+
public void viewOnline() throws IOException {
36+
Response response = ChatClient.viewOnline();
37+
System.out.println("[" + response + "]");
38+
System.out.println(response.body().string());
39+
assertEquals(200, response.code());
40+
}
41+
42+
@Test
43+
public void say() throws IOException {
44+
Response response = ChatClient.say(MY_NAME_IN_CHAT, MY_MESSAGE_TO_CHAT);
45+
System.out.println("[" + response + "]");
46+
System.out.println(response.body().string());
47+
assertEquals(200, response.code());
48+
}
49+
}

0 commit comments

Comments
 (0)