Documentation ¶
Overview ¶
Package gotest is a simple package for testing APIs.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ContentTypeJson = "application/json" // ContentTypeJson is for APIs with Json content. ContentTypeXml = "application/xml" // ContentTypeXml is for APIs with Xml content. ContentTypeForm = "application/x-www-form-urlencoded" // ContentTypeForm is for APIs with Form content. ContentTypeText = "text/plain" // ContentTypeText is for APIs with Text content. ContentTypeHtml = "text/html" // ContentTypeHtml is for APIs with Html content. ContentTypePdf = "application/pdf" // ContentTypePdf is for APIs with Pdf content. ContentTypeZip = "application/zip" // ContentTypeZip is for APIs with Zip content. ContentTypePng = "image/png" // ContentTypePng is for APIs with Png content. ContentTypeJpg = "image/jpeg" // ContentTypeJpg is for APIs with Jpg content. ContentTypeGif = "image/gif" // ContentTypeGif is for APIs with Gif content. ContentTypeSvg = "image/svg+xml" // ContentTypeSvg is for APIs with Svg content. ContentTypeBmp = "image/bmp" // ContentTypeBmp is for APIs with Bmp content. ContentTypeTiff = "image/tiff" // ContentTypeTiff is for APIs with Tiff content. ContentTypePpt = "application/vnd.ms-powerpoint" // ContentTypePpt is for APIs with Ppt content. ContentTypeDoc = "application/msword" // ContentTypeDoc is for APIs with Doc content. ContentTypeXls = "application/vnd.ms-excel" // ContentTypeXls is for APIs with Xls content. ContentTypeCsv = "text/csv" // ContentTypeCsv is for APIs with Csv content. ContentTypeXml2 = "application/xml; charset=utf-8" // ContentTypeXml2 is for APIs with Xml2 content. ContentTypeHtml2 = "text/html; charset=utf-8" // ContentTypeHtml2 is for APIs with Html2 content. )
Functions ¶
func GeneratePathParam ¶
GeneratePathParam function takes a map of string key-value pairs as input and generates a string representation of the path parameters for an API request.
Example Usage:
getParam := map[string]string{ "key1": "value1", "key2": "value2", } pathParams := GeneratePathParam(getParam) fmt.Println(pathParams)
Types ¶
type Request ¶
type Request struct { Details string // Details is the description of the test. Url string // Url is the URL of the API. Method string // Method is the HTTP method of the API. BodyType interface{} // BodyType is the type of the body of the API. BodyParam interface{} // BodyParam is the body of the API. QueryParam interface{} // QueryParam is the query of the API. Header map[string]interface{} // Header is the header of the API. ExpectedStatus int // ExpectedStatus is the expected status of the API. }
Request is the struct for the request.
type Test ¶
type Test struct { TotalTests int64 PassedTests int64 FailedTests int64 Result map[int64]Result Server *httptest.Server ServerMux *http.ServeMux }
Test is the struct for the test.
func Initialize ¶
func Initialize() *Test
Initialize initializes the test.
This returns a pointer to a Test struct. The function initializes the struct with default values and creates a new HTTP server using httptest.NewServer.
Example Usage:
test := Initialize()
func (*Test) Add ¶
Add method is a function that adds a new test case to the Test struct. It sends an HTTP request to a specified URL with the given parameters, and then checks the response against the expected status code.
Example Usage:
test := Test{} test.Add(Request{ Details: "Test Case 1", Url: "/api/test", Method: "GET", BodyType: "application/json", BodyParam: nil, QueryParam: nil, Header: nil, ExpectedStatus: 200, })
func (*Test) Dump ¶
Dump function method is responsible for printing the test results in a formatted manner.
Response time status: - **Best**: Optimal performance, ideal user experience, and minimal latency [0 - 100 ms] - **Good**: Acceptable performance with minor delays, generally not noticeable to users [100 - 300 ms] - **Acceptable**: Noticeable delays, but still within acceptable limits for most users [300 - 1000 ms (1s)] - **Poor**: Significant delays, negatively impacting user experience [1 - 2 seconds] - **Worst**: Unacceptable performance, leading to frustration and potential abandonment of the application [> 2 seconds] - **Invalid**: Errors or timeout conditions where the API fails to respond within a reasonable time frame [Timeout or Error]
Example Usage:
test.Dump(true)