Skip to content

Commit

Permalink
Pass filename to the server. (#1050)
Browse files Browse the repository at this point in the history
This is just a *hack*. but it works.

square/retrofit#1140
  • Loading branch information
tokuhirom authored Aug 21, 2023
1 parent a8c41b6 commit c3448de
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2023 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.bot.audience.client;

import static com.github.tomakehurst.wiremock.client.WireMock.aMultipart;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.put;
import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

import java.net.URI;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.bridge.SLF4JBridgeHandler;

import com.github.tomakehurst.wiremock.WireMockServer;

import com.linecorp.bot.client.base.UploadFile;

@ExtendWith(MockitoExtension.class)
@Timeout(5)
public class ManageAudienceBlobClientExTest {
static {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}

private WireMockServer wireMockServer;
private ManageAudienceBlobClient target;

@BeforeEach
public void setUp() {
wireMockServer = new WireMockServer(wireMockConfig().dynamicPort());
wireMockServer.start();
configureFor("localhost", wireMockServer.port());

target = ManageAudienceBlobClient.builder("MY_OWN_TOKEN")
.apiEndPoint(URI.create(wireMockServer.baseUrl()))
.build();
}

@AfterEach
public void tearDown() {
wireMockServer.stop();
}

@Test
public void testAddUserIdsToAudience() {
stubFor(put(urlEqualTo("/v2/bot/audienceGroup/upload/byFile"))
.withMultipartRequestBody(aMultipart()
.withName("file")
.withBody(equalTo("foobar"))
.withHeader("Content-Disposition", containing("filename=\"file\""))
).willReturn(
aResponse()
.withStatus(200)
.withHeader("content-type", "application/json")
.withBody("{}")));

// Do
target.addUserIdsToAudience(4649L, "Hello", UploadFile.fromString("foobar", "text/plain"))
.join();

// Verify
verify(
putRequestedFor(
urlEqualTo("/v2/bot/audienceGroup/upload/byFile")
).withHeader("Authorization", equalTo("Bearer MY_OWN_TOKEN"))
);
}
}
2 changes: 1 addition & 1 deletion templates/libraries/retrofit2/formParams.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#isFormParam}}
{{^isFile}}{{#isMultipart}}@Part{{/isMultipart}}{{^isMultipart}}@Field{{/isMultipart}}("{{baseName}}") {{{dataType}}} {{paramName}}{{/isFile}}
{{#isFile}}{{#isMultipart}}@Part("{{baseName}}"){{/isMultipart}}{{^isMultipart}}@Field("{{baseName}}"){{/isMultipart}} UploadFile {{paramName}}{{/isFile}}
{{#isFile}}{{#isMultipart}}@Part("{{baseName}}\"; filename=\"{{baseName}}"){{/isMultipart}}{{^isMultipart}}@Field("{{baseName}}"){{/isMultipart}} UploadFile {{paramName}}{{/isFile}}
{{/isFormParam}}

0 comments on commit c3448de

Please sign in to comment.