Skip to content

Commit

Permalink
fix: Return proper response in case of missing route (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Aug 6, 2020
1 parent 85b6ec8 commit add38b0
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

public class UnknownCommandException extends UiAutomator2Exception {
public UnknownCommandException() {
super("The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource");
super("The requested resource could not be found, or a request was received " +
"using an HTTP method that is not supported by the mapped resource");
}

public UnknownCommandException(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class AppiumResponse {
private final String sessionId;
private final HttpResponseStatus httpStatus;

public AppiumResponse(String sessionId, @Nullable Object value) {
public AppiumResponse(@Nullable String sessionId, @Nullable Object value) {
this.sessionId = sessionId;
this.value = value;
if (value instanceof Throwable) {
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/io/appium/uiautomator2/http/HttpServer.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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 io.appium.uiautomator2.http;

import java.util.ArrayList;
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/io/appium/uiautomator2/http/IHttpRequest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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 io.appium.uiautomator2.http;

import java.util.Map;
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/io/appium/uiautomator2/http/IHttpResponse.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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 io.appium.uiautomator2.http;

import java.nio.charset.Charset;
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/io/appium/uiautomator2/http/IHttpServlet.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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 io.appium.uiautomator2.http;

public interface IHttpServlet {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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 io.appium.uiautomator2.http;

import io.appium.uiautomator2.utils.Logger;
Expand Down
22 changes: 21 additions & 1 deletion app/src/main/java/io/appium/uiautomator2/http/ServerHandler.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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 io.appium.uiautomator2.http;

import java.util.List;

import io.appium.uiautomator2.common.exceptions.UnknownCommandException;
import io.appium.uiautomator2.http.impl.NettyHttpRequest;
import io.appium.uiautomator2.http.impl.NettyHttpResponse;
import io.appium.uiautomator2.server.AppiumServlet;
import io.appium.uiautomator2.utils.Logger;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
Expand Down Expand Up @@ -53,7 +71,9 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
}
}
if (!httpResponse.isClosed()) {
httpResponse.setStatus(404);
Object sessionId = httpRequest.data().get(AppiumServlet.SESSION_ID_KEY);
new AppiumResponse(sessionId == null ? null : (String) sessionId, new UnknownCommandException())
.renderTo(httpResponse);
httpResponse.end();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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 io.appium.uiautomator2.http;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
import io.appium.uiautomator2.http.IHttpRequest;
import io.appium.uiautomator2.http.IHttpResponse;
import io.appium.uiautomator2.http.IHttpServlet;
import io.netty.handler.codec.http.HttpResponseStatus;

public class AppiumServlet implements IHttpServlet {

Expand Down Expand Up @@ -244,15 +243,12 @@ public void handleHttpRequest(IHttpRequest request, IHttpResponse response) {
} else if ("DELETE".equals(request.method())) {
handler = findMatcher(request, deleteHandler);
}
handleRequest(request, response, handler);
if (handler != null) {
handleRequest(request, response, handler);
}
}

private void handleRequest(IHttpRequest request, IHttpResponse response,
@Nullable BaseRequestHandler handler) {
if (handler == null) {
response.setStatus(HttpResponseStatus.NOT_FOUND.code()).end();
return;
}
private void handleRequest(IHttpRequest request, IHttpResponse response, BaseRequestHandler handler) {
addHandlerAttributesToRequest(request, handler.getMappedUri());
AppiumResponse result = handler.handle(request);
handleResponse(response, result);
Expand Down

0 comments on commit add38b0

Please sign in to comment.