Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass filename to the server. #1050

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}}