Skip to content
Permalink
Browse files

Complete unit tests to cover pause and resumed states

  • Loading branch information...
adibfara committed May 24, 2019
1 parent 56933ef commit 12d64e75ef07bf134e99cd722f0822b99e26afbb
@@ -11,7 +11,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation project(":core")
implementation project(":interceptor-okhttp")
implementation project(":interceptor-okhttp-no-op")
implementation libs.kotlin
implementation libs.retrofit
implementation libs.retrofitScalars
@@ -12,6 +12,7 @@ import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.POST
import java.util.*

val websocketTowerObserver by lazy {
WebWatchTowerObserver(
@@ -29,7 +30,7 @@ fun main() {
OkHttpClient.Builder().addInterceptor(WatchTowerInterceptor())
.build()
)
.baseUrl("https://reqres.in/api/")
.baseUrl("https://reqres.in/")
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build().create(TestAPI::class.java)
@@ -38,12 +39,16 @@ fun main() {
withContext(Dispatchers.IO) {
while (true) {
try {
val call =
val call = if (Random().nextBoolean()) {

retrofit.getExample(
"of course", SampleRequestBody(
"test id", "TheSNAKY"
)
).execute()
} else {
retrofit.getAnotherExample().execute()
}
println(call.body() ?: " Empty response:\n" + call.message())

} catch (t: Throwable) {
@@ -62,6 +67,9 @@ fun main() {

data class SampleRequestBody(val id: String, val name: String)
interface TestAPI {
@POST("users")
@POST("api/users")
fun getExample(@Header("test") header: String, @Body requestBody: SampleRequestBody): Call<String>

@GET("404URL")
fun getAnotherExample(): Call<String>
}
@@ -12,7 +12,7 @@ Add the dependencies to your project:
```groovy
implementation 'com.snakyapps.watchtower:core:1.0.0'
debugImplementation 'com.snakyapps.watchtower:interceptor-okhttp:1.0.0'
releaseImplementation 'com.snakyapps.watchtower:interceptor-okhttp-no-op:1.0.0' //add no-op dependency for non-debug build variants
releaseImplementation 'com.snakyapps.watchtower:interceptor-okhttp-no-op:1.0.0' // no-op dependency for non-debug build variants
```

Setup
@@ -33,23 +33,27 @@ Setup
WatchTower.start(WebWatchTowerObserver(port = 8085)) // in Application class
```

Note: you should call `watchTower.shutDown()` whenever your application is terminating to close the WatchTower service and its related thread.
Note: you should call `watchTower.shutDown()` whenever your application/activity is terminating, which shuts down WatchTower-related services.
If you're using it on Android, It's best to start and shut down the WatchTower inside an Android service (using `onCreate` and `onDestroy` of an Android Service)

**Observing the events using your browser**
Connect the device that is running the app to the same network that you want to observe the data first. So if you are on a WiFi, both devices should be connected to the same WiFi so they can communicate with each other.
After running your application, you can navigate to `http://yourip:8085/` and view the network calls. If you don't know `yourip`, It will be printed out to the console with the message of `WatchTower started, listening on ... `.

#### Features
Features
--------
![Video](https://raw.githubusercontent.com/adibfara/Watchtower/master/screenshots/video.gif "Watchtower Video")

- Track and observe all API calls made through OKHttp's client
- GET, POST, PUT, DELETE, PATCH methods
- Query parameters, request and response body and headers
- Response success and failure status, size, date and latency
- Adjustable port for the server
- Adjustable port for the server and the websocket server
- API call history, even If no browsers were open
- Search in the URLs of all requests
- Fully responsive UI
- Fully extensible


**Observing the events using your browser**
Connect the device that is running the app to the same network that you want to observe the data. If you are on a WiFi, both devices should be connected to the same WiFi so they can communicate with each other.
After running your application, you can navigate to `http://yourip:8085/` and view the network calls. If you don't know what `yourip` is, It will be printed out to the console with the message of `WatchTower started, listening on ... ` when `Watchtower.start()` is called.

#### Notes
PRs are more than welcome, and please file an issue If you encounter something 🍻.
@@ -7,7 +7,7 @@ import com.snakydesign.watchtower.models.TowerObserver
/**
* @author Adib Faramarzi (adibfara@gmail.com)
*/
class WebWatchTowerObserver(port: Int) : TowerObserver() {
class WebWatchTowerObserver(port: Int = 0, websocketPort: Int = 5003) : TowerObserver() {

override fun showRequest(requestSent: RequestData) {

@@ -13,7 +13,7 @@ import java.net.InetAddress
/**
* @author Adib Faramarzi (adibfara@gmail.com)
*/
class WebWatchTowerObserver constructor(private val port: Int) : TowerObserver(),
class WebWatchTowerObserver constructor(private val port: Int, private val websocketPort: Int = 5003) : TowerObserver(),
WatchTowerWebSocketServer.MessageHandler {


@@ -60,7 +60,7 @@ class WebWatchTowerObserver constructor(private val port: Int) : TowerObserver()

websocketThread = Thread {
websocketServer = WatchTowerWebSocketServer(
InetSocketAddress(5003),
InetSocketAddress(websocketPort),
this@WebWatchTowerObserver
)
websocketServer.start()
@@ -41,6 +41,7 @@ function getRequestHTML(response) {
classname = isSuccess ? 'success' : 'failure';
success = isSuccess ? "SUCCESS" : "FAILED";
responseLength = parseInt(response.contentLength);
if (responseLength === undefined || responseLength < 0) responseLength = 0;
contentLength = responseLength > 1000 ? parseInt(response.contentLength / 1000) + " kilobytes" : responseLength + " bytes";
return htmlToElement('<div class="animatable request card ' + classname + '" id="' + id + '">' +
' <div class="url">' +
@@ -189,15 +190,15 @@ function connectToWebSocket() {
if ("WebSocket" in window) {

var wesocketAddress = "ws://" + location.hostname + ":5003/";
console.log(wesocketAddress);

// Let us open a web socket
var ws = new WebSocket(wesocketAddress);

ws.onopen = function () {

// Web Socket is connected, send data using send()
ws.send('{"connected": true}');
console.log("Message is sent...");

};

ws.onmessage = function (evt) {
@@ -218,7 +219,7 @@ function connectToWebSocket() {
ws.onclose = function () {

// websocket is closed.
console.log("Connection is closed...");

};
} else {

@@ -238,7 +239,7 @@ $("#url_search").on('keyup', function () {
});

function applySearch() {
console.log('searching');

var value = $("#url_search").val().toLowerCase();
$(".card").each(function () {
if ($(this).find(".url h2").text().toLowerCase().search(value) > -1) {
BIN +1.59 MB screenshots/video.gif
Binary file not shown.

0 comments on commit 12d64e7

Please sign in to comment.
You can’t perform that action at this time.