func TestPutWithBackend(t *testing.T) { uuid, _ := util.NewUUID() storage, _, put := fileServer(uuid) defer storage.Close() baseServer.StorageBaseURL = storage.URL defer func() { baseServer.StorageBaseURL = "" }() server := httptest.NewServer(baseServer.router()) defer server.Close() transport := &http.Transport{} client := &http.Client{Transport: transport} registrar := broker.NewRedisRegistrar() registrar.Register(uuid) // uuid = curl -XPUT <url>/streams/1/2/3 request, _ := http.NewRequest("POST", server.URL+"/streams/"+uuid, bytes.NewReader([]byte("hello world"))) request.TransferEncoding = []string{"chunked"} resp, err := client.Do(request) defer resp.Body.Close() assert.Nil(t, err) assert.Equal(t, resp.StatusCode, 200) assert.Equal(t, <-put, []byte("hello world")) }
func (s *Server) createStream(w http.ResponseWriter, r *http.Request) { registrar := broker.NewRedisRegistrar() if err := registrar.Register(key(r)); err != nil { http.Error(w, "Unable to create stream. Please try again.", http.StatusServiceUnavailable) rollbar.Error(rollbar.ERR, fmt.Errorf("unable to register stream: %#v", err)) util.CountWithData("put.create.fail", 1, "error=%s", err) return } util.Count("put.create.success") w.WriteHeader(http.StatusCreated) }
func TestPut(t *testing.T) { server := httptest.NewServer(baseServer.router()) defer server.Close() transport := &http.Transport{} client := &http.Client{Transport: transport} // uuid = curl -XPUT <url>/streams/1/2/3 request, _ := http.NewRequest("PUT", server.URL+"/streams/1/2/3", nil) resp, err := client.Do(request) defer resp.Body.Close() assert.Nil(t, err) assert.Equal(t, resp.StatusCode, http.StatusCreated) registrar := broker.NewRedisRegistrar() assert.True(t, registrar.IsRegistered("1/2/3")) }
func TestPubClosed(t *testing.T) { uuid, _ := util.NewUUID() registrar := broker.NewRedisRegistrar() err := registrar.Register(uuid) assert.Nil(t, err) writer, err := broker.NewWriter(uuid) assert.Nil(t, err) writer.Close() request, _ := http.NewRequest("POST", "/streams/"+uuid, nil) request.TransferEncoding = []string{"chunked"} response := httptest.NewRecorder() baseServer.publish(response, request) assert.Equal(t, response.Code, http.StatusNotFound) }
func mkstream(w http.ResponseWriter, _ *http.Request) { registrar := broker.NewRedisRegistrar() uuid, err := util.NewUUID() if err != nil { http.Error(w, "Unable to create stream. Please try again.", http.StatusServiceUnavailable) rollbar.Error(rollbar.ERR, fmt.Errorf("unable to create new uuid for stream: %#v", err)) util.CountWithData("mkstream.create.fail", 1, "error=%s", err) return } if err := registrar.Register(uuid); err != nil { http.Error(w, "Unable to create stream. Please try again.", http.StatusServiceUnavailable) rollbar.Error(rollbar.ERR, fmt.Errorf("unable to register stream: %#v", err)) util.CountWithData("mkstream.create.fail", 1, "error=%s", err) return } util.Count("mkstream.create.success") io.WriteString(w, string(uuid)) }