aboutsummaryrefslogtreecommitdiffstats
path: root/AndroidAsyncTest/src/com/koushikdutta/async/test/HttpServerTests.java
blob: df117e64f7e7d3a75fc0b52ec31587a095859f40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.koushikdutta.async.test;

import com.koushikdutta.async.AsyncServer;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.AsyncHttpPost;
import com.koushikdutta.async.http.body.JSONObjectBody;
import com.koushikdutta.async.http.body.MultipartFormDataBody;
import com.koushikdutta.async.http.body.StringBody;
import com.koushikdutta.async.http.body.UrlEncodedFormBody;
import com.koushikdutta.async.http.server.AsyncHttpServer;
import com.koushikdutta.async.http.server.AsyncHttpServerRequest;
import com.koushikdutta.async.http.server.AsyncHttpServerResponse;
import com.koushikdutta.async.http.server.HttpServerRequestCallback;
import com.koushikdutta.async.util.StreamUtility;

import junit.framework.TestCase;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

public class HttpServerTests extends TestCase {
    AsyncHttpServer httpServer;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        httpServer = new AsyncHttpServer();
        httpServer.setErrorCallback(new CompletedCallback() {
            @Override
            public void onCompleted(Exception ex) {
                fail();
            }
        });
        httpServer.listen(AsyncServer.getDefault(), 5000);
        
        httpServer.get("/hello", new HttpServerRequestCallback() {
            @Override
            public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
                assertNotNull(request.getHeaders().getHost());
                response.send("hello");
            }
        });

        httpServer.post("/echo", new HttpServerRequestCallback() {
            @Override
            public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
                try {
                    assertNotNull(request.getHeaders().getHost());
                    JSONObject json = new JSONObject();
                    if (request.getBody() instanceof UrlEncodedFormBody) {
                        UrlEncodedFormBody body = (UrlEncodedFormBody)request.getBody();
                        for (NameValuePair pair: body.get()) {
                            json.put(pair.getName(), pair.getValue());
                        }
                    }
                    else if (request.getBody() instanceof JSONObjectBody) {
                        json = ((JSONObjectBody)request.getBody()).get();
                    }
                    else if (request.getBody() instanceof StringBody) {
                        json.put("foo", ((StringBody)request.getBody()).get());
                    }
                    else if (request.getBody() instanceof MultipartFormDataBody) {
                        MultipartFormDataBody body = (MultipartFormDataBody)request.getBody();
                        for (NameValuePair pair: body.get()) {
                            json.put(pair.getName(), pair.getValue());
                        }
                    }

                    response.send(json);
                }
                catch (Exception e) {
                }
            }
        });
    }

    public void testJSONObject() throws Exception {
        JSONObject json = new JSONObject();
        json.put("foo", "bar");
        JSONObjectBody body = new JSONObjectBody(json);
        AsyncHttpPost post = new AsyncHttpPost("http://localhost:5000/echo");
        post.setBody(body);
        json = AsyncHttpClient.getDefaultInstance().executeJSONObject(post, null).get();
        assertEquals(json.getString("foo"), "bar");
    }

    public void testString() throws Exception {
        StringBody body = new StringBody("bar");
        AsyncHttpPost post = new AsyncHttpPost("http://localhost:5000/echo");
        post.setBody(body);
        JSONObject json = AsyncHttpClient.getDefaultInstance().executeJSONObject(post, null).get();
        assertEquals(json.getString("foo"), "bar");
    }

    public void testUrlEncodedFormBody() throws Exception {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("foo", "bar"));
        HttpPost post = new HttpPost("http://localhost:5000/echo");
        post.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse response = new DefaultHttpClient().execute(post);
        String contents = StreamUtility.readToEnd(response.getEntity().getContent());
        JSONObject json = new JSONObject(contents);
        assertEquals(json.getString("foo"), "bar");
    }
    
    public void testServerHello() throws Exception {
        URL url = new URL("http://localhost:5000/hello");
        URLConnection conn = url.openConnection();
        
        InputStream is = conn.getInputStream();
        
        String contents = StreamUtility.readToEnd(is);
        is.close();
        assertEquals(contents, "hello");
    }
    
    public void testServerHelloAgain() throws Exception {
        URL url = new URL("http://localhost:5000/hello");
        URLConnection conn = url.openConnection();
        
        InputStream is = conn.getInputStream();
        
        String contents = StreamUtility.readToEnd(is);
        is.close();
        assertEquals(contents, "hello");
    }
    
    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        
        httpServer.stop();
        AsyncServer.getDefault().stop();
    }
}