|
| 1 | +package com.howtoprogram.retrofit2; |
| 2 | + |
| 3 | +import okhttp3.MediaType; |
| 4 | +import okhttp3.MultipartBody; |
| 5 | +import okhttp3.RequestBody; |
| 6 | +import org.junit.Test; |
| 7 | +import retrofit2.Call; |
| 8 | +import retrofit2.Response; |
| 9 | +import retrofit2.Retrofit; |
| 10 | +import retrofit2.converter.gson.GsonConverterFactory; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertTrue; |
| 18 | + |
| 19 | +public class Retrofit2Test { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testPostObject() throws IOException { |
| 23 | + |
| 24 | + Retrofit retrofit = new Retrofit.Builder() |
| 25 | + .baseUrl("http://httpbin.org/") |
| 26 | + .addConverterFactory(GsonConverterFactory.create()) |
| 27 | + .build(); |
| 28 | + |
| 29 | + BookResourceRetrofit2 bookResource = retrofit.create(BookResourceRetrofit2.class); |
| 30 | + Book book = new Book(1l, "Java How To Program", "Paul Deitel"); |
| 31 | + |
| 32 | + Call<Book> call = bookResource.addBook(book); |
| 33 | + Response<Book> response = call.execute(); |
| 34 | + |
| 35 | + assertTrue(response.isSuccessful()); |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testPostJson() throws IOException { |
| 41 | + |
| 42 | + Retrofit retrofit = new Retrofit.Builder() |
| 43 | + .baseUrl("http://httpbin.org/") |
| 44 | + .addConverterFactory(GsonConverterFactory.create()) |
| 45 | + .build(); |
| 46 | + |
| 47 | + BookResourceRetrofit2 bookResource = retrofit.create(BookResourceRetrofit2.class); |
| 48 | + Book book = new Book(1l, "Java How To Program", "Paul Deitel"); |
| 49 | + |
| 50 | + Call<Book> call = bookResource.addBook(book); |
| 51 | + Response<Book> response = call.execute(); |
| 52 | + |
| 53 | + assertTrue(response.isSuccessful()); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testPostForm() throws IOException { |
| 59 | + Retrofit retrofit = new Retrofit.Builder() |
| 60 | + .baseUrl("http://httpbin.org/") |
| 61 | + .addConverterFactory(GsonConverterFactory.create()) |
| 62 | + .build(); |
| 63 | + |
| 64 | + BookResourceRetrofit2 service = retrofit.create(BookResourceRetrofit2.class); |
| 65 | + |
| 66 | + Call<Book> call = service.updateBook(1l, "Tom1", "Riddle1"); |
| 67 | + |
| 68 | + Response<Book> response = call.execute(); |
| 69 | + assertTrue(response.isSuccessful()); |
| 70 | + |
| 71 | + |
| 72 | + } |
| 73 | + @Test |
| 74 | + public void testPostFormWithFieldMap() throws IOException { |
| 75 | + Retrofit retrofit = new Retrofit.Builder() |
| 76 | + .baseUrl("http://httpbin.org/") |
| 77 | + .addConverterFactory(GsonConverterFactory.create()) |
| 78 | + .build(); |
| 79 | + |
| 80 | + BookResourceRetrofit2 service = retrofit.create(BookResourceRetrofit2.class); |
| 81 | + Map<String, String> fieldsMap = new HashMap<>(); |
| 82 | + fieldsMap.put("author","Joshua Bloch"); |
| 83 | + fieldsMap.put("name","Java puzzlers"); |
| 84 | + fieldsMap.put("id","1"); |
| 85 | + Call<Book> call = service.updateBook(fieldsMap); |
| 86 | + |
| 87 | + Response<Book> response = call.execute(); |
| 88 | + assertTrue(response.isSuccessful()); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testPostMultipart() throws IOException { |
| 94 | + Retrofit retrofit = new Retrofit.Builder() |
| 95 | + .baseUrl("http://httpbin.org/") |
| 96 | + .addConverterFactory(GsonConverterFactory.create()) |
| 97 | + .build(); |
| 98 | + |
| 99 | + BookResourceRetrofit2 service = retrofit.create(BookResourceRetrofit2.class); |
| 100 | + |
| 101 | + // create RequestBody instance from file |
| 102 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 103 | + File file = new File(classLoader.getResource("RestImage.png").getFile()); |
| 104 | + |
| 105 | + RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); |
| 106 | + |
| 107 | + // MultipartBody.Part is used to send also the actual file name |
| 108 | + MultipartBody.Part body = |
| 109 | + MultipartBody.Part.createFormData("file", file.getName(), reqFile); |
| 110 | + |
| 111 | + // add book id part within the multipart request |
| 112 | + RequestBody id = RequestBody.create(MediaType.parse("text/plain"), String.valueOf(1l)); |
| 113 | + |
| 114 | + Call<Book> call = service.addBookCover(id, body); |
| 115 | + |
| 116 | + Response<Book> response = call.execute(); |
| 117 | + assertTrue(response.isSuccessful()); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testPostMultipart2() throws IOException { |
| 123 | + Retrofit retrofit = new Retrofit.Builder() |
| 124 | + .baseUrl("http://httpbin.org/") |
| 125 | + .addConverterFactory(GsonConverterFactory.create()) |
| 126 | + .build(); |
| 127 | + |
| 128 | + BookResourceRetrofit2 service = retrofit.create(BookResourceRetrofit2.class); |
| 129 | + |
| 130 | + // create RequestBody instance from file |
| 131 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 132 | + File file = new File(classLoader.getResource("RestImage.png").getFile()); |
| 133 | + |
| 134 | + RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); |
| 135 | + |
| 136 | + RequestBody id = RequestBody.create(MediaType.parse("text/plain"), String.valueOf(1l)); |
| 137 | + |
| 138 | + Call<Book> call = service.addBookCover2(id, reqFile); |
| 139 | + |
| 140 | + Response<Book> response = call.execute(); |
| 141 | + assertTrue(response.isSuccessful()); |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments