Selaa lähdekoodia

add read me and fix test

Nhu Nguyen 6 vuotta sitten
vanhempi
commit
901786aed6

+ 54
- 0
README.md Näytä tiedosto

@@ -0,0 +1,54 @@
1
+# Java Server
2
+The training wheels are coming off....sort of. In this lab, I set up the basic server for you. Your task is to handle other type of responses. You can refactor the code however you see fit and I highly advise you to do so.
3
+
4
+Read the wiki page [HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) and figure out how to write the response for each of the request. I wrote a sample one for you to see. In `src/main/java/Main.java`, run the `main` method. Once the server is up, go `http://localhost:5000`, note it says `Hello world`.
5
+
6
+Your task for this lab is to read the [HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) and figure out how to generate the proper response. Remember to write unit test.
7
+
8
+## Notes
9
+I will not answer the following questions:
10
+- What is a HTTP?
11
+- How do I construct an HTTP response?
12
+- What's an HTTP method?
13
+- What's an HTTP code?
14
+
15
+If you come to me, I will show you [LMGTFY](http://lmgtfy.com/)(Let me google that for you). I will answer anything related to why your code doesn't work. I will also answer design related questions. I will also help you construct the test if you know what message you want to write.
16
+
17
+## Text response
18
+1. Add a `/hello` path which will return `world` as a response
19
+2. Add a `/form?key=value` path which will return the value as a response. For example if I call `/form?search=blue`, the server will return `blue`. If I call `/form?color=red`, the server will return `red`. The response code should be `201`.
20
+3. Add a `/404` path which will `Are you lost?` with `404` response code.
21
+2. Edit the `/form?key=value` path so it will only response if it is a `POST` request. Any other type of request will a `404` response.
22
+
23
+## Redirect
24
+1. Add a `/redirect` path which will redirect you to `http://google.com`
25
+
26
+## Asynchronous requests
27
+Currently our server can handle one request at a time. This is called Synchronous. We want multiple users to access the server at the same time. This is called Asynchronous. Read up on the term [here](https://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean).
28
+
29
+1. In the `run` method of the `RequestHandler`, add ` Thread.sleep(5000);` before the try and catch to make your server go to sleep for 5s before processing your request.
30
+  1. Open up your browser and go to `http://localhost:5000`
31
+  2. Open another tab and go to `http://localhost:5000`
32
+  3. Note how long it takes for the second to get a response. That's because it has to wait for the first one to complete, then it will process the second request.
33
+2. Look into the [ExecutorService](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html) to see how you can use it to handle asynchronous requests
34
+
35
+## Image response
36
+### Part 1
37
+1. Add a new file in the resources folder
38
+2. Add a new path `/files/[image_name]` (e.g. `/files/high5.gif`) will show the image
39
+
40
+### Part 2
41
+1. Add a `/files` path. The files path will list out all the files in the `src/main/resources` folder in a list.
42
+2. Clicking on any of the file names will display the content of the file. If it is a picture, then show it as a picture.
43
+
44
+## Bonus - ORM
45
+1. Add ORMlite with a mysql database to your dependencies
46
+2. Add a User class with two fields `long id` and `String name`
47
+3. Add the dao necessary to create retreive the user
48
+  1. Create 3 users
49
+4. Add `/users` path to display all the users name in a list.
50
+5. Add `/users/1` to show information for the first user (their name and id)
51
+
52
+#### Super bonus
53
+1. Add `/users/new` path to show a form where user can enter a name to create a new user.
54
+2. Add `/users` with a `POST` method to accept the form information and create the user. Redirect to `/users/[user_id]`

+ 1
- 2
src/main/java/com/zipcoder/server/RequestHandler.java Näytä tiedosto

@@ -1,6 +1,5 @@
1 1
 package com.zipcoder.server;
2 2
 
3
-import sun.jvm.hotspot.HelloWorld;
4 3
 
5 4
 import java.io.IOException;
6 5
 import java.net.Socket;
@@ -16,7 +15,7 @@ public class RequestHandler {
16 15
     public void run() {
17 16
         try {
18 17
             handleRequest();
19
-        } catch (IOException e) {
18
+        } catch (Exception e) {
20 19
             Logger.error("Cannot handle request because " + e.getMessage(), e);
21 20
         } finally {
22 21
             closeSocket();

+ 0
- 2
src/main/java/com/zipcoder/server/ResponseHello.java Näytä tiedosto

@@ -20,8 +20,6 @@ public class ResponseHello {
20 20
         httpResponse.append("\r\n");
21 21
         httpResponse.append(responseMessage);
22 22
 
23
-        System.out.println(httpResponse.toString());
24
-
25 23
         this.outputStream.write(httpResponse.toString().getBytes());
26 24
     }
27 25
 

+ 8
- 1
src/test/java/com/zipcoder/server/RequestHandlerTest.java Näytä tiedosto

@@ -17,10 +17,17 @@ public class RequestHandlerTest {
17 17
 
18 18
     @Test
19 19
     public void testRun() {
20
+        String expectedResponse = "HTTP/1.1 200 OK\n"
21
+                                    + "text/html\n"
22
+                                    + "Content-Length: 13\n"
23
+                                    + "\r\n"
24
+                                    + "Hello, world!";
25
+
26
+
20 27
         socket.setRequest("GET / HTTP/1.1");
21 28
 
22 29
         handler.run();
23
-        Assert.assertTrue(socket.getResponse().contains("HTTP/1.1 200 OK"));
30
+        Assert.assertEquals(expectedResponse, socket.getResponse());
24 31
     }
25 32
 
26 33
     @Test