예제 #1
0
func TestResponseBufferChanged(t *testing.T) {
	buf2 := NewBuffer(nil)
	buf1 := NewBuffer(buf2)
	_, req := helper.NewTestRequest("GET", "/")
	write("hi").ServeHTTP(buf1, req)
	buf1.FlushAll()

	if buf1.BodyString() != "hi" {
		t.Errorf("body string of buf1 should be \"hi\" but is :%#v", buf1.BodyString())
	}

	if buf2.BodyString() != "hi" {
		t.Errorf("body string of buf2 should be \"hi\" but is :%#v", buf2.BodyString())
	}

	if string(buf1.Body()) != "hi" {
		t.Errorf("body of buf1 should be \"hi\" but is :%#v", string(buf1.Body()))
	}

	if string(buf2.Body()) != "hi" {
		t.Errorf("body of buf2 should be \"hi\" but is :%#v", string(buf2.Body()))
	}

	if buf1.Code != 0 {
		t.Errorf("Code of buf1 should be %d but is :%d", 0, buf1.Code)
	}

	if buf2.Code != 0 {
		t.Errorf("Code of buf2 should be %d but is :%d", 0, buf2.Code)
	}

	ctype1 := buf1.Header().Get("Content-Type")
	if ctype1 != "text/plain" {
		t.Errorf("Content-Type of buf1 should be %#v but is: %#v", "text/plain", ctype1)
	}

	ctype2 := buf2.Header().Get("Content-Type")
	if ctype2 != "text/plain" {
		t.Errorf("Content-Type of buf2 should be %#v but is: %#v", "text/plain", ctype2)
	}

	if !buf1.HasChanged() {
		t.Error("buf1 should be changed, but is not")
	}

	if !buf2.HasChanged() {
		t.Error("buf2 should be changed, but is not")
	}

	if !buf1.IsOk() {
		t.Error("buf1 should be ok, but is not")
	}

	if !buf2.IsOk() {
		t.Error("buf2 should be ok, but is not")
	}
}
예제 #2
0
func TestResponseBufferWriteTo(t *testing.T) {
	rec, req := helper.NewTestRequest("GET", "/")
	buf := NewBuffer(rec)
	write("hi").ServeHTTP(buf, req)
	buf.FlushAll()
	err := helper.AssertResponse(rec, "hi", 200)
	if err != nil {
		t.Error(err)
	}
}
예제 #3
0
func TestResponseBufferWriteToStatus(t *testing.T) {
	rec, req := helper.NewTestRequest("GET", "/")
	buf := NewBuffer(rec)
	http.NotFoundHandler().ServeHTTP(buf, req)
	buf.FlushAll()
	err := helper.AssertResponse(rec, "404 page not found", 404)
	if err != nil {
		t.Error(err)
	}

	if buf.IsOk() {
		t.Error("buf is ok, but should be not")
	}
}
예제 #4
0
func TestResponseBufferStatusCreate(t *testing.T) {
	writeCreate := func(rw http.ResponseWriter, req *http.Request) {
		rw.WriteHeader(201)
	}

	buf := NewBuffer(nil)
	_, req := helper.NewTestRequest("GET", "/")
	writeCreate(buf, req)

	if buf.Code != 201 {
		t.Errorf("Code of buf should be %d but is :%d", 201, buf.Code)
	}

	if !buf.IsOk() {
		t.Error("buf should be ok, but is not")
	}
}
예제 #5
0
func TestResponseBufferNotChanged(t *testing.T) {
	buf1 := NewBuffer(nil)
	buf2 := NewBuffer(nil)
	_, req := helper.NewTestRequest("GET", "/")
	NoOp(buf1, req)
	buf1.FlushAll()

	if buf1.HasChanged() {
		t.Error("buf1 is changed, but should not be")
	}

	if buf2.HasChanged() {
		t.Error("buf2 is changed, but should not be")
	}

	if !buf1.IsOk() {
		t.Error("buf1 should be ok, but is not")
	}

	if !buf2.IsOk() {
		t.Error("buf2 should be ok, but is not")
	}
}
예제 #6
0
func TestResponseBufferReset(t *testing.T) {
	buf := NewBuffer(nil)
	_, req := helper.NewTestRequest("GET", "/")
	write("hi").ServeHTTP(buf, req)

	buf.Reset()
	if buf.Code != 0 {
		t.Errorf("wrong code, expecting 0, got %d", buf.Code)
	}

	if len(buf.header) != 0 {
		t.Errorf("header is not empty")
	}

	if buf.BodyString() != "" {
		t.Errorf("body is not empty")
	}

	if buf.HasChanged() {
		t.Errorf("HasChanged should return false")
	}

}