http_handler_test.go
http_handler_test.go - Overview
- Overview
This file contains unit tests for the HTTP handler functions related to query preparation. Specifically, it tests the prepareQuery
function, ensuring it correctly handles various scenarios such as empty queries, queries with and without variables, and potentially malicious queries.
- Detailed Documentation
TestPrepareQuery
-
Purpose: Tests the
prepareQuery
function, which is responsible for processing HTTP requests to prepare database queries, including variable substitution. It covers different scenarios like empty queries, queries with variables, and potential security concerns. -
Parameters:
t
(*testing.T): The testing object, used for reporting test failures and successes.
-
Returns: None
-
Inside
TestPrepareQuery
, a structtestCase
is defined:name
(string): Name of the test case.postData
(*model.DashboardVars): The data sent in the HTTP request body.query
(string): The expected resulting query after processing.expectedErr
(bool): Whether an error is expected during query preparation.errMsg
(string): The expected error message (or a substring of it).
-
The test cases cover scenarios like:
- Empty query
- Query with no variables
- Query with variables
- Query with variables and empty value
- Query containing "alter table" (to test for SQL injection prevention)
- Query that causes template execution error
- Variables containing array
- Mixed types of variables
-
The test function iterates through the test cases, creates an HTTP request with the
postData
as the request body, callsprepareQuery
, and validates the result against the expectedquery
andexpectedErr
.
-
- Code Examples
None.
- Clarity and Accuracy
The documentation is derived directly from the code and aims to be precise.
- Markdown & MDX Perfection
All markdown syntax is verified.
- Edge Cases To Avoid Breaking MDX
All special characters are properly escaped.
- Getting Started Relevance
Include in Getting Started: NO