Skip to content

Commit

Permalink
Solves issue sanathp#10.
Browse files Browse the repository at this point in the history
Add 2 options to request config:
 * redirectLimit - Limit for redirects performed while doing HTTP request
 * targetUrl - Expected request url after all redirects has been done
  • Loading branch information
gepo committed Mar 21, 2017
1 parent 29e3a6e commit 5c8dd5f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
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,
"redirectLimit":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
| redirectLimit | Limit for redirects performed while doing HTTP request
| targetUrl | Expected request url after all redirects has been done


## Notifications
Expand Down
47 changes: 37 additions & 10 deletions requests/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ const (
)

type RequestConfig struct {
Id int
Url string `json:"url"`
RequestType string `json:"requestType"`
Headers map[string]string `json:"headers"`
FormParams map[string]string `json:"formParams"`
UrlParams map[string]string `json:"urlParams"`
ResponseCode int `json:"responseCode"`
ResponseTime int64 `json:"responseTime"`
CheckEvery time.Duration `json:"checkEvery"`
Id int
Url string `json:"url"`
RequestType string `json:"requestType"`
Headers map[string]string `json:"headers"`
FormParams map[string]string `json:"formParams"`
UrlParams map[string]string `json:"urlParams"`
ResponseCode int `json:"responseCode"`
ResponseTime int64 `json:"responseTime"`
CheckEvery time.Duration `json:"checkEvery"`
RedirectLimit int `json:"redirectimit"`
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.RedirectLimit < 1 {
return nil
}

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

return nil
}

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

getResponse, respErr := client.Do(request)
Expand Down Expand Up @@ -296,6 +312,13 @@ 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 {
return errTargetUrl(targetUrl, requestConfig.TargetUrl)
}
}

elapsed := time.Since(start)

//Request succesfull . Add infomartion to Database
Expand Down Expand Up @@ -366,3 +389,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))
}
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,
"redirectLimit": 1,
"targetUrl": "https://golang.org/"
}
]
}

0 comments on commit 5c8dd5f

Please sign in to comment.