Esempio n. 1
0
func (interrogator *RequestInterrogator) InterrogateRequest(request *http.Request) map[string]string {
	params := make(map[string]string)

	queryParams := interrogator.interrogateParams(request.URL.Query())
	templateParams := interrogator.interrogatePath(request.URL.Path)
	pageUrl := getPageUrl(request)

	for key, value := range queryParams {
		params["param:"+key] = value
	}

	for key, value := range templateParams {
		params["param:"+key] = value
	}

	for key, value := range request.URL.Query() {
		queryValues := value[0]
		for _, val := range value[1:] {
			queryValues = queryValues + "," + val
		}
		params["query:"+key] = queryValues
	}

	for key, value := range request.Header {
		headerValues := value[0]
		for _, val := range value[1:] {
			headerValues = headerValues + "," + val
		}
		params["header:"+strings.ToLower(key)] = headerValues
	}

	for _, value := range request.Cookies() {
		params["cookie:"+value.Name] = value.Value
	}

	if interrogator.configCdn != nil && interrogator.configCdn.URL != "" {
		params["cdn:url"] = interrogator.configCdn.URL
	}

	params["server:local"] = interrogator.configParams.Servers.Local

	//TODO: Fix Environment being missing
	params["environment"] = "development"

	params["url:href"] = pageUrl

	encodedParams := make(map[string]string)
	for key, value := range params {
		encodedValue, _ := util.EncodeUrl(value)
		encodedParams[key+":encoded"] = encodedValue
	}

	for key, value := range encodedParams {
		params[key] = value
	}

	return params
}
	var configuration config.Config
	err = jsonParser.Decode(&configuration)
	configParams = configuration.Parameters
	configCdn = configuration.Cdn
	Expect(err).NotTo(HaveOccurred())
})

var _ = Describe("Request Interrogator", func() {

	It("should generate the url object", func() {
		req, _ := http.NewRequest("GET", "/teaching-resource/Queen-Elizabeth-II-Diamond-jubilee-2012-6206420", nil)
		req.Header.Add("host", "localhost:5000")
		interrogator := NewRequestInterrogator(&configParams, &configCdn)
		params := interrogator.InterrogateRequest(req)
		expectedPageUrl := "http://localhost:5000/teaching-resource/Queen-Elizabeth-II-Diamond-jubilee-2012-6206420"
		encodedExpectedPageUrl, _ := util.EncodeUrl(expectedPageUrl)
		Expect(params).To(HaveKeyWithValue("url:href", expectedPageUrl))
		Expect(params).To(HaveKeyWithValue("url:href:encoded", encodedExpectedPageUrl))
	})

	It("should extract parameters from the query", func() {
		req, _ := http.NewRequest("GET", "/teaching-resource?storyCode=2206421", nil)
		req.Header.Add("host", "localhost:5000")
		testConfig := &config.Parameters{Query: []config.Query{config.Query{"storyCode", "resourceId"}}}
		interrogator := NewRequestInterrogator(testConfig, &configCdn)
		params := interrogator.InterrogateRequest(req)
		Expect(params).To(HaveKeyWithValue("param:resourceId", "2206421"))
	})

	It("should only extract parameters from the query when they're not empty", func() {
		req, _ := http.NewRequest("GET", "/teaching-resource?storycode=2206421", nil)