Exemple #1
0
//render html page
func renderTemplate(w http.ResponseWriter, name string, data interface{}) {
	tmpl, ok := templates[name]
	if !ok {
		http.Error(http.ResponseWriter(w), fmt.Sprintf("The template %s does not exist.", name), http.StatusInternalServerError)
	}
	err := tmpl.ExecuteTemplate(w, "base", &data)
	if err != nil {
		http.Error(http.ResponseWriter(w), err.Error(), http.StatusInternalServerError)
	}
}
Exemple #2
0
func Bundle(handlers ...http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		for _, h := range handlers {
			bw := responseWriter{ResponseWriter: w}

			h.ServeHTTP(http.ResponseWriter(&bw), r)

			if bw.status != 0 {
				break
			}
		}
	})
}
Exemple #3
0
func TestGetHandlerClientDisconnect(t *testing.T) {
	defer func(was bool) {
		enforcePermissions = was
	}(enforcePermissions)
	enforcePermissions = false

	defer func(orig *bufferPool) {
		bufs = orig
	}(bufs)
	bufs = newBufferPool(1, BlockSize)
	defer bufs.Put(bufs.Get(BlockSize))

	KeepVM = MakeTestVolumeManager(2)
	defer KeepVM.Close()

	if err := KeepVM.AllWritable()[0].Put(TestHash, TestBlock); err != nil {
		t.Error(err)
	}

	resp := &notifyingResponseRecorder{
		ResponseRecorder: httptest.NewRecorder(),
		closer:           make(chan bool, 1),
	}
	if _, ok := http.ResponseWriter(resp).(http.CloseNotifier); !ok {
		t.Fatal("notifyingResponseRecorder is broken")
	}
	// If anyone asks, the client has disconnected.
	resp.closer <- true

	ok := make(chan struct{})
	go func() {
		req, _ := http.NewRequest("GET", fmt.Sprintf("/%s+%d", TestHash, len(TestBlock)), nil)
		(&LoggingRESTRouter{MakeRESTRouter()}).ServeHTTP(resp, req)
		ok <- struct{}{}
	}()

	select {
	case <-time.After(20 * time.Second):
		t.Fatal("request took >20s, close notifier must be broken")
	case <-ok:
	}

	ExpectStatusCode(t, "client disconnect", http.StatusServiceUnavailable, resp.ResponseRecorder)
	for i, v := range KeepVM.AllWritable() {
		if calls := v.(*MockVolume).called["GET"]; calls != 0 {
			t.Errorf("volume %d got %d calls, expected 0", i, calls)
		}
	}
}
Exemple #4
0
func BenchmarkGeezy(b *testing.B) {
	b.StopTimer()
	someBytes := make([]byte, 1024*1024*64)
	b.SetBytes(int64(len(someBytes)))

	w := http.ResponseWriter(&testWriter{})
	gz := gzip.NewWriter(w)
	defer gz.Close()
	w = &geezyWriter{w, gz}

	b.StartTimer()
	for i := 0; i < b.N; i++ {
		io.Copy(w, bytes.NewReader(someBytes))
	}
}
Exemple #5
0
func TestSetUserID(t *testing.T) {

	var sut = NewContextAuthenticator()
	var req = &http.Request{}
	var rsw = http.ResponseWriter(nil)

	sut.SetUserID(rsw, req, "test")
	if actual := sut.UserID(req); actual != "test" {
		t.Fatalf("After being set to '%v', userID was '%v'", "test", actual)
	}
	if !sut.IsAuthenticated(req) {
		t.Fatalf("After setting userID, user was not authenticated")
	}

}
func TestLoggingResponseWriterImplementsCloseNotifier(t *testing.T) {
	http.ResponseWriter(&LoggingResponseWriter{}).(http.CloseNotifier).CloseNotify()
}
Exemple #7
0
	if rc, ok := r.(io.ReadCloser); ok {
		return &CountingReadCloser{ReadCloser: rc}
	}
	return &CountingReadCloser{ReadCloser: struct {
		io.Reader
		io.Closer
	}{r, ioutil.NopCloser(nil)}}
}

func (cr *CountingReadCloser) Read(p []byte) (n int, err error) {
	n, err = cr.ReadCloser.Read(p)
	cr.Size += int64(n)
	return
}

var _ = http.ResponseWriter((*CountingResponseWriter)(nil))

type CountingResponseWriter struct {
	http.ResponseWriter
	Code int
	Size int64
}

func NewCountingRW(w http.ResponseWriter) *CountingResponseWriter {
	return &CountingResponseWriter{ResponseWriter: w}
}

func (cw *CountingResponseWriter) Write(p []byte) (n int, err error) {
	n, err = cw.ResponseWriter.Write(p)
	cw.Size += int64(n)
	return