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

Fix invalid data test #385

Merged
merged 3 commits into from
May 3, 2018
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
1 change: 1 addition & 0 deletions lib/src/main/java/io/ably/lib/types/BaseMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public void encode(ChannelOptions opts) throws AblyException {
encoding = ((encoding == null) ? "" : encoding + "/") + "utf-8";
}
} else if(!(data instanceof byte[])) {
Log.d(TAG, "Message data must be either `byte[]`, `String` or `JSONElement`; implicit coercion of other types to String is deprecated");
throw AblyException.fromErrorInfo(new ErrorInfo("Invalid message data or encoding", 400, 40013));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -825,27 +825,31 @@ static class MessagesEncodingDataItem {
public void reject_invalid_message_data() throws AblyException {
HashMap<String, Integer> data = new HashMap<String, Integer>();
Message message = new Message("event", data);
final Log.LogHandler logHandler = Log.handler;
try {
final ArrayList<LogLine> capturedLog = new ArrayList<>();
Log.setHandler(new Log.LogHandler() {
@Override
public void println(int severity, String tag, String msg, Throwable tr) {
capturedLog.add(new LogLine(severity, tag, msg, tr));
}
});
Log.LogHandler originalLogHandler = Log.handler;
int originalLogLevel = Log.level;
Log.setLevel(Log.DEBUG);
final ArrayList<LogLine> capturedLog = new ArrayList<>();
Log.setHandler(new Log.LogHandler() {
@Override
public void println(int severity, String tag, String msg, Throwable tr) {
capturedLog.add(new LogLine(severity, tag, msg, tr));
}
});

try {
message.encode(null);

} catch (AblyException e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should also be a catch block for the case that a Throwable is thrown that isn't an AblyException and fail the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 6999580

assertEquals(null, message.encoding);
assertEquals(data, message.data);

assertEquals(1, capturedLog.size());
LogLine capturedLine = capturedLog.get(0);
assertTrue(capturedLine.tag.contains("ably"));
assertTrue(capturedLine.msg.contains("Message data must be either `byte[]`, `String` or `JSONElement`; implicit coercion of other types to String is deprecated" ));
assertTrue(capturedLine.msg.contains("Message data must be either `byte[]`, `String` or `JSONElement`; implicit coercion of other types to String is deprecated"));
} catch (Throwable t) {
fail("reject_invalid_message_data: Unexpected exception");
} finally {
Log.setHandler(logHandler);
Log.setHandler(originalLogHandler);
Log.setLevel(originalLogLevel);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ public void presencehistory_time_f() {
(new ChannelWaiter(channel)).waitFor(ChannelState.attached);
assertEquals("Verify attached state reached", channel.state, ChannelState.attached);

/* send batches of messages with shprt inter-message delay */
/* send batches of messages with short inter-message delay */
CompletionSet msgComplete = new CompletionSet();
for(int i = 0; i < 20; i++) {
channel.presence.enter(String.valueOf(i), msgComplete.add());
Expand Down