@Test public void onlyOneEncodingIsAllowedMultipartFirst() { class Example { @Multipart // @FormUrlEncoded // @POST("/") // Call<ResponseBody> method() { return null; } } try { buildRequest(Example.class); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( "Only one encoding annotation is allowed.\n for method Example.method"); } }
@Test public void onlyOneEncodingIsAllowedFormEncodingFirst() { class Example { @FormUrlEncoded // @Multipart // @POST("/") // Call<ResponseBody> method() { return null; } } try { buildRequest(Example.class); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( "Only one encoding annotation is allowed.\n for method Example.method"); } }
@Test public void multipartFailsOnNonBodyMethod() { class Example { @Multipart // @GET("/") // Call<ResponseBody> method() { return null; } } try { buildRequest(Example.class); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( "Multipart can only be specified on HTTP methods with request body (e.g., @POST).\n for method Example.method"); } }
@Test public void multipartFailsWithNoParts() { class Example { @Multipart // @POST("/") // Call<ResponseBody> method() { return null; } } try { buildRequest(Example.class); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( "Multipart method must contain at least one @Part.\n for method Example.method"); } }
@Test public void multipartIterableRequiresName() { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@Part List<RequestBody> part) { return null; } } try { buildRequest(Example.class, new Object[] { null }); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( "@Part annotation must supply a name or use MultipartBody.Part parameter type. (parameter #1)\n" + " for method Example.method"); } }
@Test public void multipartArrayRequiresName() { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@Part RequestBody[] part) { return null; } } try { buildRequest(Example.class, new Object[] { null }); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( "@Part annotation must supply a name or use MultipartBody.Part parameter type. (parameter #1)\n" + " for method Example.method"); } }
@Test public void multipartOkHttpPartForbidsName() { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@Part("name") MultipartBody.Part part) { return null; } } try { buildRequest(Example.class, new Object[] { null }); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( "@Part parameters using the MultipartBody.Part must not include a part name in the annotation. (parameter #1)\n" + " for method Example.method"); } }
@Test public void multipartIterableOkHttpPart() { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@Part("ping") List<MultipartBody.Part> part) { return null; } } try { buildRequest(Example.class, new Object[] { null }); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( "@Part parameters using the MultipartBody.Part must not include a part name in the annotation. (parameter #1)\n" + " for method Example.method"); } }
@Test public void multipartArrayOkHttpPart() { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@Part("ping") MultipartBody.Part[] part) { return null; } } try { buildRequest(Example.class, new Object[] { null }); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( "@Part parameters using the MultipartBody.Part must not include a part name in the annotation. (parameter #1)\n" + " for method Example.method"); } }
@Test public void multipartPartMapRejectsOkHttpPartValues() { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@PartMap Map<String, MultipartBody.Part> parts) { return null; } } try { buildRequest(Example.class, new Object[] { null }); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( "@PartMap values cannot be MultipartBody.Part. Use @Part List<Part> or a different value type instead. (parameter #1)\n" + " for method Example.method"); } }
@Test public void multipartPartMapRejectsNull() { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@PartMap Map<String, RequestBody> parts) { return null; } } try { buildRequest(Example.class, new Object[] { null }); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage("Part map was null."); } }
@Test public void multipartPartMapRejectsNullValues() { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@PartMap Map<String, RequestBody> parts) { return null; } } Map<String, RequestBody> params = new LinkedHashMap<>(); params.put("ping", RequestBody.create(null, "pong")); params.put("kit", null); try { buildRequest(Example.class, params); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage("Part map contained null value for key 'kit'."); } }
@Test public void multipartPartMapMustBeMap() { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@PartMap List<Object> parts) { return null; } } try { buildRequest(Example.class, Collections.emptyList()); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( "@PartMap parameter type must be Map. (parameter #1)\n for method Example.method"); } }
@Test public void multipartNullRemovesPart() throws IOException { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@Part("ping") String ping, @Part("fizz") String fizz) { return null; } } Request request = buildRequest(Example.class, "pong", null); assertThat(request.method()).isEqualTo("POST"); assertThat(request.headers().size()).isZero(); assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/"); RequestBody body = request.body(); Buffer buffer = new Buffer(); body.writeTo(buffer); String bodyString = buffer.readUtf8(); assertThat(bodyString) .contains("Content-Disposition: form-data;") .contains("name=\"ping\"") .contains("\r\npong\r\n--"); }
@Test public void multipartPartsShouldBeInOrder() throws IOException { class Example { @Multipart @POST("/foo") Call<ResponseBody> get(@Part("first") String data, @Part("second") String dataTwo, @Part("third") String dataThree) { return null; } } Request request = buildRequest(Example.class, "firstParam", "secondParam", "thirdParam"); MultipartBody body = (MultipartBody) request.body(); Buffer buffer = new Buffer(); body.writeTo(buffer); String readBody = buffer.readUtf8(); assertThat(readBody.indexOf("firstParam")).isLessThan(readBody.indexOf("secondParam")); assertThat(readBody.indexOf("secondParam")).isLessThan(readBody.indexOf("thirdParam")); }
@Test public void multipartPartMapRejectsNonStringKeys() { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@PartMap Map<Object, RequestBody> parts) { return null; } } try { buildRequest(Example.class, new Object[] { null }); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage( "@PartMap keys must be of type String: class java.lang.Object (parameter #1)\n" + " for method Example.method"); } }
@Test public void multipartPartMapRejectsNullKeys() { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@PartMap Map<String, RequestBody> parts) { return null; } } Map<String, RequestBody> params = new LinkedHashMap<>(); params.put("ping", RequestBody.create(null, "pong")); params.put(null, RequestBody.create(null, "kat")); try { buildRequest(Example.class, params); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage("Part map contained null key."); } }
@Test public void multipartPartMapSupportsSubclasses() throws IOException { class Foo extends HashMap<String, String> { } class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@PartMap Foo parts) { return null; } } Foo foo = new Foo(); foo.put("hello", "world"); Request request = buildRequest(Example.class, foo); Buffer buffer = new Buffer(); request.body().writeTo(buffer); assertThat(buffer.readUtf8()) .contains("name=\"hello\"") .contains("\r\n\r\nworld\r\n--"); }
@Multipart @POST("pictureup/uploadshitu") Observable<ProgressBean<String>> uploadPic( @Part("pos") String pos, @Part("uptype") String uptype, @Part("fm") String index, @Part("image\"; filename=\"xxx.jpg") File image );
@Test public void multipartIterable() throws IOException { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@Part("ping") List<String> ping) { return null; } } Request request = buildRequest(Example.class, Arrays.asList("pong1", "pong2")); assertThat(request.method()).isEqualTo("POST"); assertThat(request.headers().size()).isZero(); assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/"); RequestBody body = request.body(); Buffer buffer = new Buffer(); body.writeTo(buffer); String bodyString = buffer.readUtf8(); assertThat(bodyString) .contains("Content-Disposition: form-data;") .contains("name=\"ping\"\r\n") .contains("\r\npong1\r\n--"); assertThat(bodyString) .contains("Content-Disposition: form-data;") .contains("name=\"ping\"") .contains("\r\npong2\r\n--"); }
@Test public void multipartWithEncoding() throws IOException { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@Part(value = "ping", encoding = "8-bit") String ping, @Part(value = "kit", encoding = "7-bit") RequestBody kit) { return null; } } Request request = buildRequest(Example.class, "pong", RequestBody.create( MediaType.parse("text/plain"), "kat")); assertThat(request.method()).isEqualTo("POST"); assertThat(request.headers().size()).isZero(); assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/"); RequestBody body = request.body(); Buffer buffer = new Buffer(); body.writeTo(buffer); String bodyString = buffer.readUtf8(); assertThat(bodyString) .contains("Content-Disposition: form-data;") .contains("name=\"ping\"\r\n") .contains("Content-Transfer-Encoding: 8-bit") .contains("\r\npong\r\n--"); assertThat(bodyString) .contains("Content-Disposition: form-data;") .contains("name=\"kit\"") .contains("Content-Transfer-Encoding: 7-bit") .contains("\r\nkat\r\n--"); }
@Test public void multipartPartMapWithEncoding() throws IOException { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@PartMap(encoding = "8-bit") Map<String, RequestBody> parts) { return null; } } Map<String, RequestBody> params = new LinkedHashMap<>(); params.put("ping", RequestBody.create(null, "pong")); params.put("kit", RequestBody.create(null, "kat")); Request request = buildRequest(Example.class, params); assertThat(request.method()).isEqualTo("POST"); assertThat(request.headers().size()).isZero(); assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/"); RequestBody body = request.body(); Buffer buffer = new Buffer(); body.writeTo(buffer); String bodyString = buffer.readUtf8(); assertThat(bodyString) .contains("Content-Disposition: form-data;") .contains("name=\"ping\"\r\n") .contains("Content-Transfer-Encoding: 8-bit") .contains("\r\npong\r\n--"); assertThat(bodyString) .contains("Content-Disposition: form-data;") .contains("name=\"kit\"") .contains("Content-Transfer-Encoding: 8-bit") .contains("\r\nkat\r\n--"); }
/** * 附近 模块 api */ //附近的人 改变在线状态 @Multipart @POST("api.php?m=member&a=userStatus") Observable<HttpResult<String>> setOnlineState(@Part("userid") String userid, @Part("type") String type);
@Multipart @POST("records") Call<Record> createRecord( @Header("Authorization") String token, @Part("title") String title, @Part("duration") String duration, @Part("filename") String filename, @Part("is_korean") Boolean isKorean, @Part("description") RequestBody description, @Part MultipartBody.Part file );
@Test public void multipartPartOptional() { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@Part("ping") RequestBody ping) { return null; } } try { buildRequest(Example.class, new Object[] { null }); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).isEqualTo("Multipart body must have at least one part."); } }
private void parseMethodAnnotation(Annotation annotation) { if (annotation instanceof DELETE) { parseHttpMethodAndPath("DELETE", ((DELETE) annotation).value(), false); } else if (annotation instanceof GET) { parseHttpMethodAndPath("GET", ((GET) annotation).value(), false); } else if (annotation instanceof HEAD) { parseHttpMethodAndPath("HEAD", ((HEAD) annotation).value(), false); if (!Void.class.equals(responseType)) { throw methodError("HEAD method must use Void as response type."); } } else if (annotation instanceof PATCH) { parseHttpMethodAndPath("PATCH", ((PATCH) annotation).value(), true); } else if (annotation instanceof POST) { parseHttpMethodAndPath("POST", ((POST) annotation).value(), true); } else if (annotation instanceof PUT) { parseHttpMethodAndPath("PUT", ((PUT) annotation).value(), true); } else if (annotation instanceof OPTIONS) { parseHttpMethodAndPath("OPTIONS", ((OPTIONS) annotation).value(), false); } else if (annotation instanceof HTTP) { HTTP http = (HTTP) annotation; parseHttpMethodAndPath(http.method(), http.path(), http.hasBody()); } else if (annotation instanceof retrofit2.http.Headers) { String[] headersToParse = ((retrofit2.http.Headers) annotation).value(); if (headersToParse.length == 0) { throw methodError("@Headers annotation is empty."); } headers = parseHeaders(headersToParse); } else if (annotation instanceof Multipart) { if (isFormEncoded) { throw methodError("Only one encoding annotation is allowed."); } isMultipart = true; } else if (annotation instanceof FormUrlEncoded) { if (isMultipart) { throw methodError("Only one encoding annotation is allowed."); } isFormEncoded = true; } }
@Test public void simpleMultipart() throws IOException { class Example { @Multipart // @POST("/foo/bar/") // Call<ResponseBody> method(@Part("ping") String ping, @Part("kit") RequestBody kit) { return null; } } Request request = buildRequest(Example.class, "pong", RequestBody.create( MediaType.parse("text/plain"), "kat")); assertThat(request.method()).isEqualTo("POST"); assertThat(request.headers().size()).isZero(); assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/"); RequestBody body = request.body(); Buffer buffer = new Buffer(); body.writeTo(buffer); String bodyString = buffer.readUtf8(); assertThat(bodyString) .contains("Content-Disposition: form-data;") .contains("name=\"ping\"\r\n") .contains("\r\npong\r\n--"); assertThat(bodyString) .contains("Content-Disposition: form-data;") .contains("name=\"kit\"") .contains("\r\nkat\r\n--"); }