Ejemplo n.º 1
0
func TestRequestToken(t *testing.T) {
	suggestion := client.NewSuggestion("http://google.com")
	resp, err := suggestion.RequestToken(testclient, net.IPv4(85, 225, 60, 122), "SE")
	if err != nil {
		t.Error(err)
	}
	if !resp.Ok {
		t.Error(resp)
	}
}
Ejemplo n.º 2
0
func TestSendSample(t *testing.T) {
	suggestion := client.NewSuggestion("http://google2.com")
	resp, err := suggestion.RequestToken(testclient, net.IPv4(85, 225, 60, 122), "SE")
	if err != nil {
		t.Error(err)
	}
	if !resp.Ok {
		t.Error(resp)
	}
	log.Printf("resp: %+v", resp)
	suggestion.AddSample("DNSQuery", "{}")
	n, err := suggestion.SendSamples(testclient)
	if err != nil {
		t.Error(err)
	}

	if n != 1 {
		t.Errorf("expected n==1, n=%d", n)
	}

}
Ejemplo n.º 3
0
// CreateSuggestion .
func CreateSuggestion(w rest.ResponseWriter, r *rest.Request) {

	form := shared.BrowserSuggestionTokenRequest{}
	err := r.DecodeJsonPayload(&form)
	if err != nil {
		// apiError(w, err.Error(), http.StatusInternalServerError)
		apiutils.WriteRestError(w, err)
		return
	}

	var invalids fielderrors.ValidationErrorList

	// parse and validate url.
	URL := strings.TrimSpace(form.URL)
	if URL == "" {
		invalids = append(invalids, fielderrors.NewFieldRequired("URL"))
		// apiError(w, "no or empty URL", http.StatusBadRequest)

	}
	u, err := url.Parse(URL)
	if err != nil {
		invalids = append(invalids, fielderrors.NewFieldInvalid("URL", URL, err.Error()))
		// apiError(w, fmt.Sprintf("%s is not a valid URL", URL), http.StatusBadRequest)

	}

	host := u.Host
	if strings.Contains(host, ":") {
		host, _, err = net.SplitHostPort(u.Host)
		if err != nil {
			invalids = append(invalids, fielderrors.NewFieldInvalid("URL", URL, err.Error()))
		}
	}

	if !shared.AcceptedURL(u) {
		invalids = append(invalids, fielderrors.NewFieldValueNotSupported("URL", URL, nil))
	}

	if len(invalids) > 0 {
		apiutils.WriteRestError(w, apierrors.NewInvalid("create-suggestion", "URL", invalids))
		return
	}

	s := client.NewSuggestion(u.String())
	defer s.DoneAddingSamples()
	measurers, err := measure.DefaultMeasurements(form.URL)
	if err != nil {
		apiutils.WriteRestError(w, apierrors.NewInternalError(err))
		return
	}

	for _, v := range measurers {
		m, err := v.Measure()
		if err != nil {
			lg.Errorf("could not measure: %s", err.Error())
		} else {
			switch m.Type() {
			case sampletypes.DNSQuery, sampletypes.HTTPHeader:
				err = s.AddMeasurement(m)
				if err != nil {
					lg.Errorln(err.Error())
					return
				}
			default:
				lg.Warningf("unsupported sample type: %s", m.Type().String())
			}
		}
	}
}