-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/ru/d10xa/groovyconsolebot/client/GetUpdates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed 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 ru.d10xa.groovyconsolebot.client; | ||
|
||
import ru.d10xa.groovyconsolebot.botapi.GetUpdatesRequest; | ||
import ru.d10xa.groovyconsolebot.botapi.GetUpdatesResponse; | ||
|
||
import java.io.IOException; | ||
|
||
public interface GetUpdates { | ||
GetUpdatesResponse get(GetUpdatesRequest request) throws IOException; | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/ru/d10xa/groovyconsolebot/client/OkHttpGetUpdates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed 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 ru.d10xa.groovyconsolebot.client; | ||
|
||
import com.google.gson.Gson; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import ru.d10xa.groovyconsolebot.botapi.GetUpdatesRequest; | ||
import ru.d10xa.groovyconsolebot.botapi.GetUpdatesResponse; | ||
|
||
import java.io.IOException; | ||
|
||
public class OkHttpGetUpdates implements GetUpdates { | ||
|
||
private final GetUpdatesHttpUrlFactory httpUrlFactory; | ||
|
||
private final OkHttpClient client; | ||
|
||
private final Gson gson; | ||
|
||
public OkHttpGetUpdates( | ||
GetUpdatesHttpUrlFactory httpUrlFactory, | ||
OkHttpClient client, | ||
Gson gson) { | ||
this.httpUrlFactory = httpUrlFactory; | ||
this.client = client; | ||
this.gson = gson; | ||
} | ||
|
||
@Override | ||
public GetUpdatesResponse get(GetUpdatesRequest getUpdatesRequest) throws IOException { | ||
Request okHttpRequest = new Request.Builder() | ||
.url(httpUrlFactory.build(getUpdatesRequest)) | ||
.build(); | ||
Response response = client.newCall(okHttpRequest).execute(); | ||
handleErrors(response); | ||
return gson.fromJson(response.body().string(), GetUpdatesResponse.class); | ||
} | ||
|
||
private void handleErrors(Response response) throws IOException { | ||
if (!response.isSuccessful()) { | ||
System.out.println(response); | ||
throw new IOException(String.valueOf(response.code())); | ||
} | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
src/test/groovy/ru/d10xa/groovyconsolebot/client/OkHttpGetUpdatesSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed 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 ru.d10xa.groovyconsolebot.client | ||
|
||
import com.google.gson.Gson | ||
import okhttp3.OkHttpClient | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import ru.d10xa.groovyconsolebot.botapi.GetUpdatesRequest | ||
import ru.d10xa.groovyconsolebot.botapi.GetUpdatesResponse | ||
import spock.lang.Specification | ||
|
||
class OkHttpGetUpdatesSpec extends Specification { | ||
|
||
MockWebServer server | ||
|
||
GetUpdatesHttpUrlFactory httpUrlFactory | ||
|
||
def setup() { | ||
server = new MockWebServer() | ||
httpUrlFactory = { server.url("/getUpdates") } | ||
} | ||
|
||
def cleanup() { | ||
server.shutdown() | ||
} | ||
|
||
def 'one message from private chat'() { | ||
given: | ||
String body = getClass().getClassLoader() | ||
.getResource("json/getUpdates_1465736691.json").text | ||
|
||
server.enqueue(new MockResponse().setBody(body)) | ||
server.start() | ||
|
||
when: | ||
GetUpdates getUpdates = new OkHttpGetUpdates(httpUrlFactory, new OkHttpClient(), new Gson()) | ||
GetUpdatesResponse updates = getUpdates.get(new GetUpdatesRequest()) | ||
|
||
def update0 = updates.result[0] | ||
|
||
then: | ||
update0.message.text == "2**3" | ||
server.getRequestCount() == 1 | ||
server.takeRequest().body.readUtf8() == "" | ||
} | ||
|
||
def 'telegram nginx bad gateway'() { | ||
given: | ||
String body = getClass().getClassLoader() | ||
.getResource("json/getUpdates_nginx_bad_gateway.html").text | ||
server.enqueue(new MockResponse() | ||
.setResponseCode(502) | ||
.setBody(body)) | ||
|
||
server.start() | ||
|
||
when: | ||
GetUpdates getUpdates = new OkHttpGetUpdates(httpUrlFactory, new OkHttpClient(), new Gson()) | ||
getUpdates.get(new GetUpdatesRequest()) | ||
|
||
then: | ||
IOException e = thrown() | ||
e.message == "502" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<html> | ||
<head><title>502 Bad Gateway</title></head> | ||
<body bgcolor="white"> | ||
<center><h1>502 Bad Gateway</h1></center> | ||
<hr><center>nginx/1.9.1</center> | ||
</body> | ||
</html> |