Skip to content
Open
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
3 changes: 2 additions & 1 deletion larksuite-oapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
<skipTests>${skipTests}</skipTests>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
<version>${maven-surefire-plugin.version}</version>
Expand Down Expand Up @@ -245,6 +245,7 @@
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
<maven-surefire-plugin.version>2.12.4</maven-surefire-plugin.version>
<skipTests>true</skipTests>
<maven-versions-plugin.version>2.7</maven-versions-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<robolectric.version>10-robolectric-5803371</robolectric.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,36 @@
package com.lark.oapi.core.utils;

import com.lark.oapi.core.response.RawResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

public class UnmarshalRespUtil {

private static final Logger log = LoggerFactory.getLogger(UnmarshalRespUtil.class);
private static final int MAX_LOG_BODY_LENGTH = 500;

public static <T> T unmarshalResp(RawResponse resp, Class<T> respClass) {
return Jsons.DEFAULT.fromJson(new String(resp.getBody(), StandardCharsets.UTF_8),
respClass);
String bodyStr = new String(resp.getBody(), StandardCharsets.UTF_8);
int statusCode = resp.getStatusCode();
if (statusCode < 200 || statusCode >= 300) {
String truncatedBody = truncate(bodyStr);
log.warn("unexpected response status, statusCode={}, body={}", statusCode, truncatedBody);
}
try {
return Jsons.DEFAULT.fromJson(bodyStr, respClass);
} catch (Exception e) {
String truncatedBody = truncate(bodyStr);
log.error("unmarshal response error, statusCode={}, body={}", statusCode, truncatedBody);
throw e;
}
}

private static String truncate(String body) {
if (body.length() > MAX_LOG_BODY_LENGTH) {
return body.substring(0, MAX_LOG_BODY_LENGTH) + "...(truncated)";
}
return body;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* MIT License
*
* Copyright (c) 2022 Lark Technologies Pte. Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice, shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.lark.oapi.core.utils;

import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
import com.lark.oapi.core.response.RawResponse;
import org.junit.Test;

import java.nio.charset.StandardCharsets;

public class TestUnmarshalRespUtil {

public static class SimpleResp {
@SerializedName("code")
private int code;
@SerializedName("msg")
private String msg;

public int getCode() { return code; }
public String getMsg() { return msg; }
}

@Test
public void testUnmarshalSuccess() {
RawResponse raw = new RawResponse();
raw.setStatusCode(200);
raw.setBody("{\"code\":0,\"msg\":\"ok\"}".getBytes(StandardCharsets.UTF_8));

SimpleResp resp = UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class);
assert resp != null;
assert resp.getCode() == 0;
assert "ok".equals(resp.getMsg());
}

@Test(expected = JsonSyntaxException.class)
public void testUnmarshalInvalidJson() {
RawResponse raw = new RawResponse();
raw.setStatusCode(502);
raw.setBody("Bad Gateway".getBytes(StandardCharsets.UTF_8));

UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class);
}

@Test(expected = JsonSyntaxException.class)
public void testUnmarshalStringInsteadOfObject() {
RawResponse raw = new RawResponse();
raw.setStatusCode(200);
raw.setBody("\"some error message\"".getBytes(StandardCharsets.UTF_8));

UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class);
}

@Test(expected = JsonSyntaxException.class)
public void testUnmarshalHtmlResponse() {
RawResponse raw = new RawResponse();
raw.setStatusCode(403);
raw.setBody("<html><body>Forbidden</body></html>".getBytes(StandardCharsets.UTF_8));

UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class);
}

@Test(expected = JsonSyntaxException.class)
public void testUnmarshalLongBodyIsTruncatedInLog() {
RawResponse raw = new RawResponse();
raw.setStatusCode(500);
StringBuilder sb = new StringBuilder("\"");
for (int i = 0; i < 600; i++) {
sb.append("x");
}
sb.append("\"");
raw.setBody(sb.toString().getBytes(StandardCharsets.UTF_8));

UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class);
}

@Test
public void testUnmarshalNon2xxWithValidJson() {
RawResponse raw = new RawResponse();
raw.setStatusCode(400);
raw.setBody("{\"code\":99991400,\"msg\":\"param error\"}".getBytes(StandardCharsets.UTF_8));

SimpleResp resp = UnmarshalRespUtil.unmarshalResp(raw, SimpleResp.class);
assert resp != null;
assert resp.getCode() == 99991400;
}
}