Exemple #1
0
func TestConnDecoderReuse(t *testing.T) {
	c := newConn(nil)
	v := struct {
		XMLName struct{} `xml:"hello"`
		Foo     string   `xml:"foo"`
	}{}

	c.reset()
	c.buf.WriteString(`<hello><foo>foo</foo></hello>`)
	st.Expect(t, c.decoder.InputOffset(), int64(0))
	c.decoder.Decode(&v)
	st.Expect(t, v.Foo, "foo")
	st.Expect(t, c.decoder.InputOffset(), int64(29))

	c.reset()
	c.buf.WriteString(`<hello><foo>bar</foo></hello>`)
	st.Expect(t, c.decoder.InputOffset(), int64(0))
	tok, _ := c.decoder.Token()
	se := tok.(xml.StartElement)
	st.Expect(t, se.Name.Local, "hello")
	tok, _ = c.decoder.Token()
	se = tok.(xml.StartElement)
	st.Expect(t, se.Name.Local, "foo")
	st.Expect(t, c.decoder.InputOffset(), int64(12))

	c.reset()
	c.buf.WriteString(`<hello><foo>blam&lt;</foo></hello>`)
	st.Expect(t, c.decoder.InputOffset(), int64(0))
	c.decoder.Decode(&v)
	st.Expect(t, v.Foo, "blam<")
	st.Expect(t, c.decoder.InputOffset(), int64(34))
}
Exemple #2
0
// Prints failure output, including the correct line number.
func TestFailedExpectationMessages(t *testing.T) {
	t.Log("Tests purposely fail to demonstrate output")
	st.Expect(t, 1, 2)
	st.Reject(t, "same", "same")
	var typedNil *string
	st.Expect(t, typedNil, nil) // in Go, a typed nil != nil
}
Exemple #3
0
func TestRequestSetHeaders(t *testing.T) {
	req := NewRequest()
	req.SetHeaders(map[string]string{"foo": "baz", "baz": "foo"})
	req.Middleware.Run("request", req.Context)
	st.Expect(t, req.Context.Request.Header.Get("foo"), "baz")
	st.Expect(t, req.Context.Request.Header.Get("baz"), "foo")
}
Exemple #4
0
func TestRequestMux(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, world")
	}))
	defer ts.Close()

	req := NewRequest()
	req.UseRequest(func(ctx *context.Context, h context.Handler) {
		u, _ := url.Parse(ts.URL)
		ctx.Request.URL = u
		h.Next(ctx)
	})

	req.Mux().AddMatcher(func(ctx *context.Context) bool {
		return ctx.GetString("$phase") == "request" && ctx.Request.Method == "GET"
	}).UseRequest(func(ctx *context.Context, h context.Handler) {
		ctx.Request.Header.Set("mux", "true")
		h.Next(ctx)
	})

	res, err := req.Send()
	st.Expect(t, err, nil)
	st.Expect(t, res.StatusCode, 200)
	st.Expect(t, res.RawRequest.Header.Get("mux"), "true")
}
Exemple #5
0
func TestDeleteRange(t *testing.T) {
	v := deleteRange([]byte(`<foo><bar><baz></baz></bar></foo>`), []byte(`<baz`), []byte(`</baz>`))
	st.Expect(t, string(v), `<foo><bar></bar></foo>`)

	v = deleteRange([]byte(`<foo><bar><baz></baz></bar></foo>`), []byte(`</bar>`), []byte(`o>`))
	st.Expect(t, string(v), `<foo><bar><baz></baz>`)
}
Exemple #6
0
func TestHello(t *testing.T) {
	c, err := NewConn(testDial(t))
	st.Assert(t, err, nil)
	err = c.Hello()
	st.Expect(t, err, nil)
	st.Expect(t, c.Greeting.ServerName, "ISPAPI EPP Server") // FIXME: brittle external dependency
}
func TestUrlBasedRetrieve_InvalidResponseFailure(t *testing.T) {
	defer gock.Off()
	gock.New("http://localhost").
		Get("/endpoint/__ids").
		Reply(200).BodyString("Invalid response")
	expectedError := errors.New("ERROR - invalid character 'I' looking for beginning of value")

	retriever := newURLBasedIDListRetriever("http://localhost/endpoint/", http.DefaultClient)
	var idsChan = make(chan string)
	var errChan = make(chan error)

	go retriever.Retrieve(idsChan, errChan)

	for idsChan != nil {
		select {
		case _, ok := <-idsChan:
			if !ok {
				idsChan = nil
			}
		case actualError := <-errChan:
			if expectedError != actualError {
				st.Expect(t, expectedError, actualError)
			}
			return
		}
	}

	st.Expect(t, gock.IsDone(), true)
}
Exemple #8
0
func TestGoogleTXT(t *testing.T) {
	r := New(0)
	rrs, err := r.ResolveErr("google.com", "TXT")
	st.Expect(t, err, nil)
	st.Expect(t, len(rrs) >= 4, true)
	st.Expect(t, count(rrs, func(rr RR) bool { return rr.Type == "TXT" }), 1)
}
Exemple #9
0
func TestBazCoUKAny(t *testing.T) {
	r := New(0)
	rrs, err := r.ResolveErr("baz.co.uk", "")
	st.Expect(t, err, nil)
	st.Expect(t, len(rrs) >= 2, true)
	st.Expect(t, count(rrs, func(rr RR) bool { return rr.Type == "NS" }) >= 2, true)
}
Exemple #10
0
func TestRequestAlreadyDispatched(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, world")
	}))
	defer ts.Close()

	req := NewRequest()
	req.UseRequest(func(ctx *context.Context, h context.Handler) {
		h.Next(ctx)
	})

	req.UseRequest(func(ctx *context.Context, h context.Handler) {
		u, _ := url.Parse(ts.URL)
		ctx.Request.URL = u
		h.Next(ctx)
	})

	res, err := req.Send()
	st.Expect(t, err, nil)
	st.Reject(t, res.RawRequest.URL, nil)
	st.Expect(t, res.StatusCode, 200)

	res, err = req.Send()
	st.Reject(t, err, nil)
}
Exemple #11
0
func TestMiddlewareErrorInjectionAndInterception(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, world")
	}))
	defer ts.Close()

	req := NewRequest()
	req.UseRequest(func(ctx *context.Context, h context.Handler) {
		h.Next(ctx)
	})

	req.UseRequest(func(ctx *context.Context, h context.Handler) {
		u, _ := url.Parse(ts.URL)
		ctx.Request.URL = u
		h.Error(ctx, errors.New("Error"))
	})

	req.UseError(func(ctx *context.Context, h context.Handler) {
		ctx.Error = nil
		h.Next(ctx)
	})

	res, err := req.Send()
	st.Expect(t, err, nil)
	st.Reject(t, res.RawRequest.URL, nil)
	st.Expect(t, res.StatusCode, 200)
}
Exemple #12
0
func TestRequestInterceptor(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		t.Fatal("Server should not be reached!")
		fmt.Fprintln(w, "Hello, world")
	}))
	defer ts.Close()

	req := NewRequest()
	req.UseRequest(func(ctx *context.Context, h context.Handler) {
		ctx.Request.Header.Set("Client", "gentleman")

		ctx.Response.StatusCode = 201
		ctx.Response.Status = "201 Created"

		ctx.Response.Header.Set("Server", "gentleman")
		utils.WriteBodyString(ctx.Response, "Hello, gentleman")

		h.Stop(ctx)
	})
	req.UseRequest(func(ctx *context.Context, h context.Handler) {
		t.Fatal("middleware should not be called")
		h.Next(ctx)
	})

	res, err := req.Do()
	st.Expect(t, err, nil)
	st.Expect(t, res.StatusCode, 201)
	st.Expect(t, res.RawRequest.Header.Get("Client"), "gentleman")
	st.Expect(t, res.RawResponse.Header.Get("Server"), "gentleman")
	st.Expect(t, res.String(), "Hello, gentleman")
}
func TestBlueOvenAny(t *testing.T) {
	r := New(0)
	rrs, err := r.ResolveErr("blueoven.com", "")
	st.Expect(t, err, nil)
	st.Expect(t, len(rrs), 2)
	st.Expect(t, count(rrs, func(rr RR) bool { return rr.Type == "NS" && rr.Name == "blueoven.com." }), 2)
}
func TestDisableCompression(t *testing.T) {
	ctx := context.New()
	fn := newHandler()
	Disable().Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)
	transport := ctx.Client.Transport.(*http.Transport)
	st.Expect(t, transport.DisableCompression, true)
}
Exemple #15
0
func TestGreetingSupportsObject(t *testing.T) {
	g := Greeting{}
	st.Expect(t, g.SupportsObject(ObjDomain), false)
	st.Expect(t, g.SupportsObject(ObjHost), false)
	g.Objects = testObjects
	st.Expect(t, g.SupportsObject(ObjDomain), true)
	st.Expect(t, g.SupportsObject(ObjHost), true)
}
Exemple #16
0
func TestGoogleAny(t *testing.T) {
	r := New(0)
	rrs, err := r.ResolveErr("google.com", "")
	st.Expect(t, err, nil)
	st.Expect(t, len(rrs) >= 1, true)
	st.Expect(t, count(rrs, func(rr RR) bool { return rr.Type == "NS" }) >= 2, true)
	st.Expect(t, count(rrs, func(rr RR) bool { return rr.Type == "A" }) >= 1, true)
}
Exemple #17
0
func TestResponseBytes(t *testing.T) {
	ctx := NewContext()
	utils.WriteBodyString(ctx.Response, "foo bar")
	res, err := buildResponse(ctx)
	body := res.Bytes()
	st.Expect(t, err, nil)
	st.Expect(t, string(body), "foo bar")
}
Exemple #18
0
func TestGreetingSupportsExtension(t *testing.T) {
	g := Greeting{}
	st.Expect(t, g.SupportsExtension(ExtCharge), false)
	st.Expect(t, g.SupportsExtension(ExtIDN), false)
	g.Extensions = testExtensions
	st.Expect(t, g.SupportsExtension(ExtCharge), true)
	st.Expect(t, g.SupportsExtension(ExtIDN), true)
}
Exemple #19
0
func TestTimeout(t *testing.T) {
	ctx := context.New()
	fn := newHandler()
	Request(1000).Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)
	st.Expect(t, ctx.Error, nil)
	st.Expect(t, int(ctx.Client.Timeout), 1000)
}
Exemple #20
0
func TestResponse_Text(t *testing.T) {
	r := NewResponse("google.com", "whois.verisign-grs.com")
	r.Charset = "utf-8"
	r.Body = []byte("hello")
	text, err := r.Text()
	st.Expect(t, err, nil)
	st.Expect(t, string(text), "hello")
}
Exemple #21
0
func TestConnClose(t *testing.T) {
	c, err := NewConn(testDial(t))
	st.Expect(t, err, nil)
	st.Reject(t, c, nil)
	st.Reject(t, c.Greeting.ServerName, "")
	err = c.Close()
	st.Expect(t, err, nil)
}
Exemple #22
0
func TestRequestClone(t *testing.T) {
	req1 := NewRequest()
	req1.UseRequest(func(c *context.Context, h context.Handler) {})
	req1.Context.Set("foo", "bar")
	req2 := req1.Clone()
	st.Expect(t, req1 != req2, true)
	st.Expect(t, req2.Context.GetString("foo"), req1.Context.GetString("foo"))
	st.Expect(t, len(req2.Middleware.GetStack()), 1)
}
Exemple #23
0
func TestSetTransport(t *testing.T) {
	ctx := context.New()
	fn := newHandler()
	transport := &http.Transport{}
	Set(transport).Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)
	newTransport := ctx.Client.Transport.(*http.Transport)
	st.Expect(t, newTransport, transport)
}
Exemple #24
0
func TestRequestFile(t *testing.T) {
	reader := bytes.NewReader([]byte("hello world"))
	req := NewRequest()
	req.File("foo", reader)
	req.Middleware.Run("request", req.Context)
	st.Expect(t, strings.Contains(req.Context.Request.Header.Get("Content-Type"), "multipart/form-data"), true)
	body, _ := ioutil.ReadAll(req.Context.Request.Body)
	st.Expect(t, strings.Contains(string(body), "hello world"), true)
}
Exemple #25
0
func TestResponseReadError(t *testing.T) {
	ctx := NewContext()
	ctx.Error = errors.New("foo error")
	res, _ := buildResponse(ctx)
	num, err := res.Read([]byte{})
	st.Reject(t, err, nil)
	st.Expect(t, err.Error(), "foo error")
	st.Expect(t, num, -1)
}
Exemple #26
0
func TestRequestJSON(t *testing.T) {
	req := NewRequest()
	req.JSON(map[string]string{"foo": "bar"})
	req.Middleware.Run("request", req.Context)
	st.Expect(t, int(req.Context.Request.ContentLength), 14)
	st.Expect(t, req.Context.Request.Header.Get("Content-Type"), "application/json")
	body, _ := ioutil.ReadAll(req.Context.Request.Body)
	st.Expect(t, string(body)[:13], `{"foo":"bar"}`)
}
Exemple #27
0
func TestRequestBodyString(t *testing.T) {
	req := NewRequest()
	req.BodyString("foo bar")
	req.Middleware.Run("request", req.Context)
	st.Expect(t, int(req.Context.Request.ContentLength), 7)
	st.Expect(t, req.Context.Request.Header.Get("Content-Type"), "")
	body, _ := ioutil.ReadAll(req.Context.Request.Body)
	st.Expect(t, string(body), "foo bar")
}
Exemple #28
0
func TestHerokuMulti(t *testing.T) {
	r := New(0)
	_, err := r.ResolveErr("us-east-1-a.route.herokuapp.com", "A")
	st.Expect(t, err, nil)
	rrs, err := r.ResolveErr("us-east-1-a.route.herokuapp.com", "TXT")
	st.Expect(t, err, nil)
	st.Expect(t, count(rrs, func(rr RR) bool { return rr.Type == "TXT" }), 0)
	st.Expect(t, count(rrs, func(rr RR) bool { return rr.Type == "A" }), 0)
}
Exemple #29
0
func TestDeleteBufferRange(t *testing.T) {
	buf := bytes.NewBufferString(`<foo><bar><baz></baz></bar></foo>`)
	deleteBufferRange(buf, []byte(`<baz`), []byte(`</baz>`))
	st.Expect(t, buf.String(), `<foo><bar></bar></foo>`)

	buf = bytes.NewBufferString(`<foo><bar><baz></baz></bar></foo>`)
	deleteBufferRange(buf, []byte(`</bar>`), []byte(`o>`))
	st.Expect(t, buf.String(), `<foo><bar><baz></baz>`)
}
Exemple #30
0
func TestQuerySet(t *testing.T) {
	ctx := context.New()
	ctx.Request.URL.RawQuery = "baz=foo&foo=foo"
	fn := newHandler()

	Set("foo", "bar").Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)
	st.Expect(t, ctx.Request.URL.RawQuery, "baz=foo&foo=bar")
}