Fix tests (#179)
Co-authored-by: James Mills <1290234+prologic@users.noreply.github.com> Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/179
This commit is contained in:
parent
ddd16c202f
commit
8cdda7118d
|
@ -497,9 +497,10 @@ func (cli *Client) SetAvatar(content []byte) error {
|
|||
|
||||
// Request makes a signed request to a broker's API.
|
||||
func (cli *Client) Request(method, endpoint string, body []byte) ([]byte, error) {
|
||||
// TODO: Automatically work out the URI based on SRV lookups of the user's address
|
||||
u := cli.Me().Endpoint()
|
||||
u.Path = endpoint
|
||||
u, err := url.Parse(endpoint)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing endpoint address %s: %w", endpoint, err)
|
||||
}
|
||||
|
||||
res, err := SignedRequest(cli.id.key, method, u.String(), nil, bytes.NewBuffer(body))
|
||||
if err != nil {
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
sync "github.com/sasha-s/go-deadlock"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
@ -158,8 +161,11 @@ func main() {
|
|||
}()
|
||||
}
|
||||
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
|
||||
log.Infof("%s v%s listening on http://%s", path.Base(os.Args[0]), saltyim.FullVersion(), bind)
|
||||
if err := svr.Run(); err != nil {
|
||||
if err := svr.Run(ctx); err != nil {
|
||||
log.WithError(err).Fatal("error running or shutting down server")
|
||||
}
|
||||
}
|
||||
|
|
2
go.mod
2
go.mod
|
@ -85,7 +85,7 @@ require (
|
|||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||
github.com/writeas/go-strip-markdown/v2 v2.1.1 // indirect
|
||||
golang.org/x/exp v0.0.0-20220328175248-053ad81199eb // indirect
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
|
||||
golang.org/x/sys v0.0.0-20220405052023-b1e9470b6e64 // indirect
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/avast/retry-go"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
@ -55,9 +56,13 @@ func TestMain(m *testing.M) {
|
|||
}
|
||||
|
||||
stop := make(chan struct{})
|
||||
|
||||
wg, ctx := errgroup.WithContext(context.Background())
|
||||
|
||||
wg.Go(svr.Run)
|
||||
wg.Go(func() error {
|
||||
return svr.Run(ctx)
|
||||
})
|
||||
|
||||
wg.Go(func() error {
|
||||
<-stop
|
||||
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
||||
|
@ -66,6 +71,19 @@ func TestMain(m *testing.M) {
|
|||
return svr.Shutdown(ctx)
|
||||
})
|
||||
|
||||
if err := retry.Do(func() error {
|
||||
_, err := http.Get(serverBaseURL + "/api/v1/ping")
|
||||
return err
|
||||
},
|
||||
retry.LastErrorOnly(true),
|
||||
retry.OnRetry(func(n uint, err error) {
|
||||
fmt.Printf("waiting for server to be ready (try #%d): %s", n, err)
|
||||
}),
|
||||
); err != nil {
|
||||
fmt.Printf("error waiting for server: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
i := m.Run()
|
||||
|
||||
close(stop)
|
||||
|
@ -112,11 +130,8 @@ func TestBlobPut(t *testing.T) {
|
|||
|
||||
cli := internal.NewTestUser("alice@localhost", serverBaseURL, t)
|
||||
|
||||
err := cli.Register(serverBaseURL)
|
||||
require.NoError(err)
|
||||
|
||||
body := []byte("Hello World!")
|
||||
_, err = cli.Request(http.MethodPut, serverBaseURL+"/api/v1/blob/test", body)
|
||||
_, err := cli.Request(http.MethodPut, serverBaseURL+"/api/v1/blob/test", body)
|
||||
require.NoError(err)
|
||||
}
|
||||
|
||||
|
@ -126,14 +141,11 @@ func TestBlobGet(t *testing.T) {
|
|||
|
||||
cli := internal.NewTestUser("alice@localhost", serverBaseURL, t)
|
||||
|
||||
err := cli.Register(serverBaseURL)
|
||||
require.NoError(err)
|
||||
|
||||
body := []byte("Hello World!")
|
||||
_, err = cli.Request(http.MethodPut, serverBaseURL+"/api/v1/blob/test", body)
|
||||
_, err := cli.Request(http.MethodPut, serverBaseURL+"/api/v1/blob/test", body)
|
||||
require.NoError(err)
|
||||
|
||||
data, err := cli.Request(http.MethodGet, serverBaseURL+"/api/v1/blob/test", nil)
|
||||
require.NoError(err)
|
||||
assert.Equal(body, data, "expected returned blob to be identical to what we stored")
|
||||
assert.Equal(body, data)
|
||||
}
|
||||
|
|
|
@ -44,13 +44,13 @@ func Sign(req *http.Request, key ed25519.PrivateKey) (*http.Request, error) {
|
|||
fmt.Fprint(h, req.Method, req.URL.Path)
|
||||
|
||||
if req.Body != nil {
|
||||
b := &bytes.Buffer{}
|
||||
w := io.MultiWriter(h, b)
|
||||
_, err := io.Copy(w, req.Body)
|
||||
buf := &bytes.Buffer{}
|
||||
mw := io.MultiWriter(h, buf)
|
||||
_, err := io.Copy(mw, req.Body)
|
||||
if err != nil {
|
||||
return req, err
|
||||
}
|
||||
req.Body = io.NopCloser(b)
|
||||
req.Body = io.NopCloser(buf)
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodEdDSA, jwt.RegisteredClaims{
|
||||
|
@ -97,6 +97,7 @@ func VerifyMiddleware(next httprouter.Handle) httprouter.Handle {
|
|||
rw.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
req.Body = io.NopCloser(buf)
|
||||
}
|
||||
|
||||
subject := enc(hash.Sum(nil))
|
||||
|
|
|
@ -3,7 +3,6 @@ package authreq_test
|
|||
import (
|
||||
"crypto/ed25519"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
|
@ -31,9 +30,6 @@ func TestGETRequest(t *testing.T) {
|
|||
req, err = authreq.Sign(req, priv)
|
||||
require.NoError(err)
|
||||
|
||||
t.Log(enc(pub))
|
||||
t.Log(req.Header.Get(authorizationHeader))
|
||||
|
||||
var hdlr httprouter.Handle = func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
c := authreq.ClaimsFromRequest(r)
|
||||
if c == nil {
|
||||
|
@ -71,9 +67,6 @@ func TestPOSTRequest(t *testing.T) {
|
|||
req, err = authreq.Sign(req, priv)
|
||||
require.NoError(err)
|
||||
|
||||
t.Log(enc(pub))
|
||||
t.Log(req.Header.Get(authorizationHeader))
|
||||
|
||||
var hdlr httprouter.Handle = func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
c := authreq.ClaimsFromRequest(r)
|
||||
if c == nil {
|
||||
|
@ -81,7 +74,6 @@ func TestPOSTRequest(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
contentCheck, err := io.ReadAll(r.Body)
|
||||
r.Body.Close()
|
||||
|
||||
if err != nil {
|
||||
|
@ -89,7 +81,6 @@ func TestPOSTRequest(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
t.Log(string(contentCheck))
|
||||
if !strings.Contains(req.URL.Path, c.Issuer) {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
|
|
|
@ -5,10 +5,8 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.mills.io/prologic/msgbus"
|
||||
|
@ -67,7 +65,7 @@ type Server struct {
|
|||
api *API
|
||||
}
|
||||
|
||||
// AddRouter ...
|
||||
// AddRoute ...
|
||||
func (s *Server) AddRoute(method, path string, handler http.Handler) {
|
||||
s.router.Handler(method, path, handler)
|
||||
}
|
||||
|
@ -95,7 +93,7 @@ func (s *Server) Shutdown(ctx context.Context) error {
|
|||
}
|
||||
|
||||
// Run ...
|
||||
func (s *Server) Run() (err error) {
|
||||
func (s *Server) Run(ctx context.Context) (err error) {
|
||||
idleConnsClosed := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
|
@ -105,9 +103,6 @@ func (s *Server) Run() (err error) {
|
|||
}
|
||||
}()
|
||||
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
|
||||
go func() {
|
||||
if err := s.svc.Run(ctx); err != nil {
|
||||
log.WithError(err).Error("error running service user")
|
||||
|
@ -115,11 +110,9 @@ func (s *Server) Run() (err error) {
|
|||
}()
|
||||
|
||||
<-ctx.Done()
|
||||
log.Infof("Received signal %s", ctx.Err())
|
||||
|
||||
log.Info("Shutting down...")
|
||||
|
||||
// We received an interrupt signal, shut down.
|
||||
if err = s.Shutdown(context.Background()); err != nil {
|
||||
// Error from closing listeners, or context timeout:
|
||||
log.WithError(err).Fatal("Error shutting down HTTP server")
|
||||
|
|
|
@ -14,7 +14,7 @@ func NewTestUser(addr, broker string, t *testing.T) *saltyim.Client {
|
|||
me, err := saltyim.ParseAddr(addr)
|
||||
require.NoError(err)
|
||||
|
||||
id, err := saltyim.CreateIdentity()
|
||||
id, err := saltyim.CreateIdentity(saltyim.WithIdentityAddr(me))
|
||||
require.NoError(err)
|
||||
|
||||
cli, err := saltyim.NewClient(saltyim.WithAddr(me), saltyim.WithIdentity(id))
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:3133c2c49ec1c697518deb6f2ffb59e9902bc07f8c6ab9f6cb558d4711bcd357
|
||||
size 29757066
|
||||
oid sha256:989e51abe9913578bd6c260d249151beebec38b7a8d184691cce2db7b42965ad
|
||||
size 29760185
|
||||
|
|
Loading…
Reference in New Issue