X Tutup
Skip to content

Commit fdca57e

Browse files
committed
Configure Basic Authentication on Unirest for Java
1 parent 9c3ecc4 commit fdca57e

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

unirest-java-examples/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# OkHttp Client Example and Tutorials
2+
3+
4+
## 1. Import source code into Eclipse
5+
6+
Menu **File –> Import –> Maven –> Existing Maven Projects**
7+
8+
Browse to your source code location
9+
10+
Click **Finish** button to finish the importing
11+
12+
13+
Source code are described:
14+
###[Download a File with OkHttp](http://howtoprogram.xyz/2016/11/17/download-a-file-with-okhttp/)
15+
###[Set Timeout with OkHttp](http://howtoprogram.xyz/2016/11/23/set-timeout-okhttp/)

unirest-java-examples/pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.howtoprogram.unirest</groupId>
6+
<artifactId>unirest-java-examples</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>Unirest Java Examples and Tutorials</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>com.mashape.unirest</groupId>
22+
<artifactId>unirest-java</artifactId>
23+
<version>1.4.9</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>commons-io</groupId>
27+
<artifactId>commons-io</artifactId>
28+
<version>2.5</version>
29+
<scope>test</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>junit</groupId>
33+
<artifactId>junit</artifactId>
34+
<version>4.12</version>
35+
</dependency>
36+
</dependencies>
37+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.howtoprogram;
2+
3+
4+
import com.mashape.unirest.http.HttpResponse;
5+
import com.mashape.unirest.http.JsonNode;
6+
import com.mashape.unirest.http.Unirest;
7+
import com.mashape.unirest.http.exceptions.UnirestException;
8+
import com.mashape.unirest.http.utils.Base64Coder;
9+
import org.junit.Test;
10+
11+
import static org.hamcrest.CoreMatchers.equalTo;
12+
import static org.junit.Assert.*;
13+
14+
import org.junit.Test;
15+
16+
import java.io.IOException;
17+
18+
import static org.junit.Assert.assertTrue;
19+
20+
public class BasicAuthenticationTest {
21+
22+
@Test
23+
public void testBasicAuthAPI() throws IOException, UnirestException {
24+
25+
HttpResponse<JsonNode> response = Unirest.get("http://httpbin.org/basic-auth/user/passwd")
26+
.basicAuth("user", "passwd")
27+
.asJson();
28+
assertThat(response.getStatus(), equalTo(200));
29+
}
30+
31+
@Test
32+
public void testBasicAuthRawHeader() throws IOException, UnirestException {
33+
34+
HttpResponse<JsonNode> response = Unirest.get("http://httpbin.org/basic-auth/user/passwd").
35+
header("Authorization", "Basic " + Base64Coder.encodeString("user" + ":" + "passwd"))
36+
.asJson();
37+
38+
assertThat(response.getStatus(), equalTo(200));
39+
}
40+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.howtoprogram;
2+
3+
import com.mashape.unirest.http.HttpResponse;
4+
import com.mashape.unirest.http.JsonNode;
5+
import com.mashape.unirest.http.Unirest;
6+
import com.mashape.unirest.http.async.Callback;
7+
import com.mashape.unirest.http.exceptions.UnirestException;
8+
import static org.junit.Assert.*;
9+
import org.junit.Test;
10+
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
import java.util.Map;
14+
import java.util.concurrent.Future;
15+
16+
/**
17+
* Created by nangs on 12/31/2016.
18+
*/
19+
public class UnirestIntroductionTest {
20+
private static final String BASE_URL = "http://httpbin.org/get";
21+
22+
23+
@Test
24+
public void getWithParameters() throws IOException, UnirestException {
25+
HttpResponse<JsonNode> response = Unirest.get(BASE_URL).queryString("username", "admin").asJson();
26+
assertTrue(response.getStatus()==200);
27+
28+
}
29+
30+
@Test
31+
public void getWithParametersVal() throws IOException, UnirestException {
32+
HttpResponse<JsonNode> response = Unirest.get(BASE_URL).queryString("username", "admin").asJson();
33+
String value = response.getBody().getObject().getJSONObject("args").get("username").toString();
34+
assertEquals(value,"admin");
35+
36+
}
37+
38+
@Test
39+
public void getASynchronously() throws IOException {
40+
Future<HttpResponse<JsonNode>> future = Unirest.get(BASE_URL).queryString("username", "admin")
41+
.header("accept", "application/json")
42+
.asJsonAsync(new Callback<JsonNode>() {
43+
44+
public void failed(UnirestException e) {
45+
fail("The request has failed");
46+
}
47+
48+
public void completed(HttpResponse<JsonNode> response) {
49+
assertTrue(response.getStatus()==200);
50+
}
51+
52+
public void cancelled() {
53+
fail("The test is cancelled");
54+
}
55+
56+
});
57+
}
58+
@Test
59+
public void postWithUnirest() throws IOException, UnirestException {
60+
HttpResponse<JsonNode> response = Unirest.get(BASE_URL).queryString("username", "admin").asJson();
61+
String value = response.getBody().getObject().getJSONObject("args").get("username").toString();
62+
assertEquals(value,"admin");
63+
64+
}
65+
66+
}

0 commit comments

Comments
 (0)
X Tutup