Beispiel #1
0
func BenchmarkUnmarshal2Fields(b *testing.B) {
	params := httprequest.Params{
		Request: &http.Request{
			Form: url.Values{
				"limit": {"2000"},
			},
		},
		PathVar: httprouter.Params{{
			Key:   "id",
			Value: "someid",
		}},
	}
	var arg testParams2Fields

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		arg = testParams2Fields{}
		err := httprequest.Unmarshal(params, &arg)
		if err != nil {
			b.Fatalf("unmarshal failed: %v", err)
		}
	}
	b.StopTimer()
	if !reflect.DeepEqual(arg, testParams2Fields{
		Id:    "someid",
		Limit: 2000,
	}) {
		b.Errorf("unexpected result: got %#v", arg)
	}
}
Beispiel #2
0
func (*unmarshalSuite) TestUnmarshal(c *gc.C) {
	for i, test := range unmarshalTests {
		c.Logf("%d: %s", i, test.about)
		t := reflect.TypeOf(test.val)
		fillv := reflect.New(t)
		err := httprequest.Unmarshal(test.params, fillv.Interface())
		if test.expectError != "" {
			c.Assert(err, gc.ErrorMatches, test.expectError)
			continue
		}
		c.Assert(fillv.Elem().Interface(), jc.DeepEquals, test.val)
	}
}
Beispiel #3
0
func BenchmarkHandle2FieldsUnmarshalOnly(b *testing.B) {
	results := []testResult{}
	benchmarkHandle2Fields(b, errorMapper.HandleJSON(func(p httprequest.Params) (interface{}, error) {
		var arg testParams2Fields
		if err := httprequest.Unmarshal(p, &arg); err != nil {
			return nil, err
		}
		if arg.Limit <= 0 {
			panic("unreachable")
		}
		return results, nil
	}))
}
Beispiel #4
0
func (d *formDischarger) form(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	if r.Form.Get("waitid") == "" {
		d.errorf(w, r, "no waitid")
		return
	}
	if r.Method == "GET" {
		if d.getError {
			httprequest.WriteJSON(w, http.StatusInternalServerError, testError)
			d.discharger.FinishInteraction(w, r, nil, testError)
			return
		}
		var sr form.SchemaResponse
		if !d.emptySchema {
			sr.Schema = environschema.Fields{
				"username": environschema.Attr{
					Type: environschema.Tstring,
				},
				"password": environschema.Attr{
					Type:   environschema.Tstring,
					Secret: true,
				},
			}
		}
		httprequest.WriteJSON(w, http.StatusOK, sr)
		return
	}
	if r.Method != "POST" {
		d.errorf(w, r, "bad method %q", r.Method)
		return
	}
	if d.postError {
		httprequest.WriteJSON(w, http.StatusInternalServerError, testError)
		d.discharger.FinishInteraction(w, r, nil, testError)
		return
	}
	var lr form.LoginRequest
	err := httprequest.Unmarshal(httprequest.Params{Request: r}, &lr)
	if err != nil {
		d.errorf(w, r, "bad login request: %s", err)
		return
	}
	d.discharger.FinishInteraction(w, r, nil, nil)
}
Beispiel #5
0
func BenchmarkUnmarshal4Fields(b *testing.B) {
	fromDate, err1 := time.Parse(dateFormat, "2010-10-10")
	toDate, err2 := time.Parse(dateFormat, "2011-11-11")
	if err1 != nil || err2 != nil {
		b.Fatalf("bad times")
	}
	type P testParams4Fields
	params := httprequest.Params{
		Request: &http.Request{
			Form: url.Values{
				"limit": {"2000"},
				"from":  {fromDate.Format(dateFormat)},
				"to":    {toDate.Format(dateFormat)},
			},
		},
		PathVar: httprouter.Params{{
			Key:   "id",
			Value: "someid",
		}},
	}
	var args P

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		args = P{}
		err := httprequest.Unmarshal(params, &args)
		if err != nil {
			b.Fatalf("unmarshal failed: %v", err)
		}
	}
	b.StopTimer()
	if !reflect.DeepEqual(args, P{
		Id:    "someid",
		Limit: 2000,
		From:  dateTime{fromDate},
		To:    dateTime{toDate},
	}) {
		b.Errorf("unexpected result: got %#v", args)
	}
}
Beispiel #6
0
func BenchmarkHandle16StringFieldsUnmarshalOnly(b *testing.B) {
	benchmarkHandleNFields(b, 16, errorMapper.HandleErrors(func(p httprequest.Params) error {
		var arg testParams16StringFields
		return httprequest.Unmarshal(p, &arg)
	}))
}