Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add requestLimit and targetUrl to request config #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
12 changes: 8 additions & 4 deletions Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ Config file should be in JSON format (Support for other formats will be added in
"requestType":"GET",
"checkEvery":30,
"responseCode":200,
"responseTime":800
"responseTime":800,
"requestLimit":2,
"targetUrl":"https://google.com"
}
.....
]
Expand Down Expand Up @@ -105,9 +107,11 @@ Description for each request parameter.
| headers | A list of key value pairs which will be added to header of a request
| formParams | A list of key value pairs which will be added to body of the request.By deafult content type is "application/x-www-form-urlencoded".For aplication/json content type add "Content-Type":"application/json" to headers
| urlParams | A list of key value pairs which will be appended to url e.g: http://google.com?name=statusok
|checkEvery| Time interval in seconds.If the value is 120,the request will be performed every 2 minutes
|responseCode|Expected response code when a request is performed.Default values is 200.If response code is not equal then an error notification is triggered.
|responseTime|Expected response time in milliseconds,when mean response time is below this value a notification is triggered
| checkEvery | Time interval in seconds.If the value is 120,the request will be performed every 2 minutes
| responseCode | Expected response code when a request is performed.Default values is 200.If response code is not equal then an error notification is triggered.
| responseTime | Expected response time in milliseconds,when mean response time is below this value a notification is triggered
| requestLimit | Limit for redirects performed while doing HTTP request
| targetUrl | Expected request url after all redirects has been done


## Notifications
Expand Down
41 changes: 39 additions & 2 deletions requests/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type RequestConfig struct {
ResponseCode int `json:"responseCode"`
ResponseTime int64 `json:"responseTime"`
CheckEvery time.Duration `json:"checkEvery"`
RequestLimit int `json:"requestLimit"`
TargetUrl string `json:"targetUrl"`
}

//Set Id for request
Expand Down Expand Up @@ -255,7 +257,21 @@ func PerformRequest(requestConfig RequestConfig, throttle chan int) error {
}
*/

client := &http.Client{}
redirectPolicyFunc := func(req *http.Request, via []*http.Request) error {
if requestConfig.RequestLimit < 1 {
return nil
}

if len(via) >= requestConfig.RequestLimit {
return errors.New(fmt.Sprintf("Requests limit %v has been reached", requestConfig.RequestLimit))
}

return nil
}

client := &http.Client{
CheckRedirect: redirectPolicyFunc,
}
start := time.Now()

getResponse, respErr := client.Do(request)
Expand All @@ -282,7 +298,7 @@ func PerformRequest(requestConfig RequestConfig, throttle chan int) error {

defer getResponse.Body.Close()

if getResponse.StatusCode != requestConfig.ResponseCode {
if requestConfig.ResponseCode > 0 && getResponse.StatusCode != requestConfig.ResponseCode {
//Response code is not the expected one .Add Error to database
go database.AddErrorInfo(database.ErrorInfo{
Id: requestConfig.Id,
Expand All @@ -296,6 +312,23 @@ func PerformRequest(requestConfig RequestConfig, throttle chan int) error {
return errResposeCode(getResponse.StatusCode, requestConfig.ResponseCode)
}

if requestConfig.TargetUrl != "" {
targetUrl := getResponse.Request.URL.String()
if targetUrl != requestConfig.TargetUrl {
go database.AddErrorInfo(database.ErrorInfo{
Id: requestConfig.Id,
Url: requestConfig.Url,
RequestType: requestConfig.RequestType,
ResponseCode: getResponse.StatusCode,
ResponseBody: convertResponseToString(getResponse),
Reason: errTargetUrl(targetUrl, requestConfig.TargetUrl),
OtherInfo: "",
})

return errTargetUrl(targetUrl, requestConfig.TargetUrl)
}
}

elapsed := time.Since(start)

//Request succesfull . Add infomartion to Database
Expand Down Expand Up @@ -366,3 +399,7 @@ func GetJsonParamsBody(params map[string]string) (io.Reader, error) {
func errResposeCode(status int, expectedStatus int) error {
return errors.New(fmt.Sprintf("Got Response code %v. Expected Response Code %v ", status, expectedStatus))
}

func errTargetUrl(targetUrl string, expectedUrl string) error {
return errors.New(fmt.Sprintf("Target url is %v. Expected url %v", targetUrl, expectedUrl))
}
38 changes: 34 additions & 4 deletions requests/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
func TestRequestsInit(t *testing.T) {

data := make([]RequestConfig, 0)
google := RequestConfig{1, "http://google.com", "GET", nil, nil, nil, 200, 100, 1}
google := RequestConfig{Id: 1, Url: "http://google.com", RequestType: "GET", ResponseCode: 200, ResponseTime: 100, CheckEvery: 1}
data = append(data, google)

RequestsInit(data, 0)
Expand All @@ -18,7 +18,7 @@ func TestRequestsInit(t *testing.T) {
}

func TestGetRequest(t *testing.T) {
google := RequestConfig{1, "http://google.com", "GET", nil, nil, nil, 200, 100, 1}
google := RequestConfig{Id: 1, Url: "http://google.com", RequestType: "GET", ResponseCode: 200, ResponseTime: 100, CheckEvery: 1}

err := PerformRequest(google, nil)

Expand All @@ -28,7 +28,7 @@ func TestGetRequest(t *testing.T) {
}

func TestInvalidGetRequest(t *testing.T) {
invalid := RequestConfig{1, "http://localhost:64521", "GET", nil, nil, nil, 200, 100, 1}
invalid := RequestConfig{Id: 1, Url: "http://localhost:64521", RequestType: "GET", ResponseCode: 200, ResponseTime: 100, CheckEvery: 1}

err := PerformRequest(invalid, nil)

Expand All @@ -38,11 +38,41 @@ func TestInvalidGetRequest(t *testing.T) {
}

func TestInvalidPostRequest(t *testing.T) {
google := RequestConfig{1, "http://google.com", "POST", nil, nil, nil, 200, 100, 1}
google := RequestConfig{Id: 1, Url: "http://google.com", RequestType: "POST", ResponseCode: 200, ResponseTime: 100, CheckEvery: 1}

err := PerformRequest(google, nil)

if err == nil {
t.Error("Invalid POST Request Succeded")
}
}

func TestRequestLimitOne(t *testing.T) {
google := RequestConfig{Id: 1, Url: "http://google.com", RequestType: "GET", RequestLimit: 1}

err := PerformRequest(google, nil)

if err == nil {
t.Error("Request limit 1 should not allow any redirects")
}
}

func TestRequestLimitBig(t *testing.T) {
google := RequestConfig{Id: 1, Url: "http://google.com", RequestType: "GET", RequestLimit: 10}

err := PerformRequest(google, nil)

if err != nil {
t.Error("Invalid checking for request limit")
}
}

func TestResponseCode(t *testing.T) {
google := RequestConfig{Id: 1, Url: "http://google.com", RequestType: "GET", ResponseCode: 404}

err := PerformRequest(google, nil)

if err == nil {
t.Error("Invalid checking for response code")
}
}
6 changes: 4 additions & 2 deletions sample_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,17 @@
"responseTime":800
},
{
"url":"https://google.com",
"url":"http://golang.org",
"requestType":"GET",
"headers":{
},
"params":{
},
"checkEvery":30,
"responseCode":200,
"responseTime":800
"responseTime":800,
"requestLimit": 1,
"targetUrl": "https://golang.org/"
}
]
}