|
| 1 | +package com.howtoprogram.okhttp.posting; |
| 2 | + |
| 3 | +import static junit.framework.Assert.assertTrue; |
| 4 | + |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileReader; |
| 8 | +import java.io.IOException; |
| 9 | + |
| 10 | +import org.junit.Ignore; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import okhttp3.FormBody; |
| 14 | +import okhttp3.MediaType; |
| 15 | +import okhttp3.MultipartBody; |
| 16 | +import okhttp3.OkHttpClient; |
| 17 | +import okhttp3.Request; |
| 18 | +import okhttp3.RequestBody; |
| 19 | +import okhttp3.Response; |
| 20 | +import okio.BufferedSink; |
| 21 | + |
| 22 | +public class OkHttpPostTest { |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testPostingWithString() throws IOException { |
| 26 | + |
| 27 | + OkHttpClient client = new OkHttpClient.Builder().build(); |
| 28 | + MediaType textPlainMT = MediaType.parse("text/plain; charset=utf-8"); |
| 29 | + String st = "Hello OkHttp"; |
| 30 | + |
| 31 | + Request request = new Request.Builder().url("http://httpbin.org/post") |
| 32 | + .post(RequestBody.create(textPlainMT, st)).build(); |
| 33 | + |
| 34 | + Response response = client.newCall(request).execute(); |
| 35 | + |
| 36 | + assertTrue(response.isSuccessful()); |
| 37 | + response.close(); |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + private static final MediaType MEDIA_TYPE_PLAINTEXT = MediaType |
| 42 | + .parse("text/plain; charset=utf-8"); |
| 43 | + private final OkHttpClient client = new OkHttpClient(); |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testPostMultipart() |
| 47 | + throws IOException { |
| 48 | + |
| 49 | + File file = new File("src/test/resources/Lorem Ipsum.txt"); |
| 50 | + RequestBody requestBody = new MultipartBody.Builder() |
| 51 | + .setType(MultipartBody.FORM) |
| 52 | + |
| 53 | + .addFormDataPart("file", "LoremIpsum.txt", |
| 54 | + RequestBody.create(MEDIA_TYPE_PLAINTEXT, file)) |
| 55 | + .addFormDataPart("description", "Just Lorem Ipsum") |
| 56 | + .build(); |
| 57 | + |
| 58 | + Request request = new Request.Builder() |
| 59 | + .url("http://httpbin.org/post") |
| 60 | + .post(requestBody).build(); |
| 61 | + |
| 62 | + Response response = client.newCall(request).execute(); |
| 63 | + if (!response.isSuccessful()) { |
| 64 | + throw new IOException("Unexpected code " + response); |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testPostForm() throws Exception { |
| 71 | + |
| 72 | + RequestBody formBody = new FormBody.Builder() |
| 73 | + .add("email", "user@howtoprogram.xyz") |
| 74 | + .add("firstname", "John").add("lastname", "Henry").build(); |
| 75 | + |
| 76 | + Request request = new Request.Builder().url("http://httpbin.org/post") |
| 77 | + .post(formBody) |
| 78 | + .build(); |
| 79 | + |
| 80 | + Response response = client.newCall(request).execute(); |
| 81 | + |
| 82 | + assertTrue(response.isSuccessful()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testPostByteArray() throws Exception { |
| 87 | + |
| 88 | + String st = "posting a byte array with OkHttp"; |
| 89 | + |
| 90 | + Request request = new Request.Builder() |
| 91 | + .url("http://httpbin.org/post") |
| 92 | + .post(RequestBody.create(MEDIA_TYPE_PLAINTEXT, st.getBytes("UTF8"))) |
| 93 | + .build(); |
| 94 | + |
| 95 | + Response response = client.newCall(request).execute(); |
| 96 | + |
| 97 | + assertTrue(response.isSuccessful()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testPostStream() throws Exception { |
| 102 | + |
| 103 | + RequestBody requestBody = new RequestBody() { |
| 104 | + @Override |
| 105 | + public MediaType contentType() { |
| 106 | + return MEDIA_TYPE_PLAINTEXT; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void writeTo(BufferedSink sink) throws IOException { |
| 111 | + |
| 112 | + File file = new File("src/test/resources/Lorem Ipsum.txt"); |
| 113 | + try (BufferedReader br = new BufferedReader(new FileReader(file))) { |
| 114 | + String line; |
| 115 | + while ((line = br.readLine()) != null) { |
| 116 | + sink.writeUtf8(line); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + }; |
| 122 | + |
| 123 | + Request request = new Request.Builder() |
| 124 | + .url("http://httpbin.org/post") |
| 125 | + .post(requestBody) |
| 126 | + .build(); |
| 127 | + |
| 128 | + Response response = client.newCall(request).execute(); |
| 129 | + |
| 130 | + assertTrue(response.isSuccessful()); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testPostFile() throws Exception { |
| 135 | + File file = new File("src/test/resources/Lorem Ipsum.txt"); |
| 136 | + |
| 137 | + Request request = new Request.Builder() |
| 138 | + .url("http://httpbin.org/post") |
| 139 | + .post(RequestBody.create(MEDIA_TYPE_PLAINTEXT, file)) |
| 140 | + .build(); |
| 141 | + |
| 142 | + Response response = client.newCall(request).execute(); |
| 143 | + |
| 144 | + assertTrue(response.isSuccessful()); |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments