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
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,29 @@ import com.chuckerteam.chucker.internal.support.distinctUntilChanged
internal class HttpTransactionDatabaseRepository(private val database: ChuckerDatabase) : HttpTransactionRepository {

private val transactionDao get() = database.transactionDao()

private val customQueryRegex = """method *: *(get|put|post|patch|delete) *"""
.toRegex(RegexOption.IGNORE_CASE)
override fun getFilteredTransactionTuples(code: String, path: String): LiveData<List<HttpTransactionTuple>> {
val pathQuery = if (path.isNotEmpty()) "%$path%" else "%"
val pathQuery = if (path.isNotEmpty() &&
!customQueryRegex.containsMatchIn(path)
) {
"%$path%"
} else if (customQueryRegex
.containsMatchIn(input = path)
) {
path.substring(path.indexOf(':') + 1).trim()
} else {
"%"
}
return transactionDao.getFilteredTuples(
"$code%",
pathQuery = pathQuery,
/**
* Refer <a href='https://github.com/ChuckerTeam/chucker/issues/847">Issue #847</a> for
* more context
*/
graphQlQuery = pathQuery
graphQlQuery = pathQuery,
methodQuery = pathQuery
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ internal interface HttpTransactionDao {
"SELECT id, requestDate, tookMs, protocol, method, host, path, scheme, responseCode, " +
"requestPayloadSize, responsePayloadSize, error, graphQLDetected, graphQlOperationName FROM " +
"transactions WHERE responseCode LIKE :codeQuery AND (path LIKE :pathQuery OR " +
"graphQlOperationName LIKE :graphQlQuery) ORDER BY requestDate DESC"
"graphQlOperationName LIKE :graphQlQuery OR method LIKE :methodQuery) ORDER BY requestDate DESC"
)
fun getFilteredTuples(
codeQuery: String,
pathQuery: String,
graphQlQuery: String = ""
graphQlQuery: String = "",
methodQuery: String = ""
): LiveData<List<HttpTransactionTuple>>

@Insert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,69 @@ internal class HttpTransactionDatabaseRepositoryTest {
assertThat(it?.host).isEqualTo(newHost)
}
}

@Test
fun `tuples are filtered based on request method`() = runBlocking {
testObject.insertTransaction(transaction.withResponseData())
testObject.insertTransaction(otherTransaction.withResponseData())

testObject.getFilteredTransactionTuples("", "method : get ").observeForever {
assertTuples(listOf(otherTransaction, transaction), it)
}
testObject.getFilteredTransactionTuples("", "method : get").observeForever {
assertTuples(listOf(otherTransaction, transaction), it)
}
testObject.getFilteredTransactionTuples("", "method : gEt").observeForever {
assertTuples(listOf(otherTransaction, transaction), it)
}
testObject.getFilteredTransactionTuples("", "method:gEt").observeForever {
assertTuples(listOf(otherTransaction, transaction), it)
}
testObject.getFilteredTransactionTuples("", "METHOD:GET ").observeForever {
assertTuples(listOf(otherTransaction, transaction), it)
}
}

@Test
fun `tuples are filtered based on request method POST`() = runBlocking {
val anotherTransaction = createRequest("abc").apply {
requestDate = 200L
responseCode = 418
method = "POST"
}

testObject.insertTransaction(transaction)
testObject.insertTransaction(otherTransaction)
testObject.insertTransaction(anotherTransaction)

testObject.getFilteredTransactionTuples("", "method : POST ").observeForever {
assertTuples(listOf(anotherTransaction), it)
}

testObject.getFilteredTransactionTuples("", "Method: PoST ").observeForever {
assertTuples(listOf(anotherTransaction), it)
}
}

@Test
fun `tuples are filtered based on request method PUT`() = runBlocking {
val anotherTransaction = createRequest("abc").apply {
requestDate = 200L
responseCode = 418
method = "POST"
}
val putTransaction = createRequest("abc").apply {
requestDate = 200L
responseCode = 418
method = "PUT"
}
testObject.insertTransaction(transaction)
testObject.insertTransaction(otherTransaction)
testObject.insertTransaction(anotherTransaction)
testObject.insertTransaction(putTransaction)

testObject.getFilteredTransactionTuples("", "method: PUT ").observeForever {
assertTuples(listOf(putTransaction), it)
}
}
}