Skip to content

Commit

Permalink
GH-257: Updated test.
Browse files Browse the repository at this point in the history
Moved tests from rescache_test packages to package test.
  • Loading branch information
jirenius committed Jul 3, 2024
1 parent 214cbb9 commit 41d01f4
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 248 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ jobs:
- name: Build
run: go build -v ./...
- name: Test
env:
RESGATE_TEST_EXTENDED: 1
run: go test -v -covermode=atomic -coverprofile=cover.out -coverpkg=./server/... ./...
- name: Install goveralls
run: go install github.com/mattn/goveralls@latest
Expand Down
2 changes: 1 addition & 1 deletion scripts/cover.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash -e
# Run from directory above via ./scripts/cover.sh

go test -v -covermode=atomic -coverprofile=./cover.out -coverpkg=./server/... ./...
env RESGATE_TEST_EXTENDED=1 go test -v -covermode=atomic -coverprofile=./cover.out -coverpkg=./server/... ./...
go tool cover -html=cover.out
2 changes: 1 addition & 1 deletion server/apiHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *Service) apiHandler(w http.ResponseWriter, r *http.Request) {

apiPath := s.cfg.APIPath

// NotFound on oaths with trailing slash (unless it is only the APIPath)
// NotFound on paths with trailing slash (unless it is only the APIPath)
if len(path) > len(apiPath) && path[len(path)-1] == '/' {
notFoundHandler(w, s.enc)
return
Expand Down
5 changes: 3 additions & 2 deletions server/httpServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
func (s *Service) initHTTPServer() {
}

// startHTTPServer initializes the server and starts a goroutine with a http server
// Service.mu is held when called
// startHTTPServer initializes the server and starts a goroutine with a http
// server Service.mu is held when called.
func (s *Service) startHTTPServer() {
if s.cfg.NoHTTP {
return
Expand Down Expand Up @@ -60,6 +60,7 @@ func (s *Service) stopHTTPServer() {
}

func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Global OPTIONS handling taken from http.ServeMux
if r.RequestURI == "*" {
if r.ProtoAtLeast(1, 1) {
w.Header().Set("Connection", "close")
Expand Down
142 changes: 0 additions & 142 deletions server/rescache/legacy_test.go

This file was deleted.

27 changes: 0 additions & 27 deletions server/wsConn.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,33 +290,6 @@ func (c *wsConn) SetVersion(protocol string) (string, error) {
return ProtocolVersion, nil
}

func (c *wsConn) GetSubscription(rid string, cb func(sub *Subscription, err error)) {
sub, err := c.Subscribe(rid, true, nil)
if err != nil {
cb(nil, err)
return
}

sub.CanGet(func(err error) {
if err != nil {
cb(nil, err)
c.Unsubscribe(sub, true, false, 1, true)
return
}

sub.OnReady(func() {
err := sub.Error()
if err != nil {
cb(nil, err)
return
}
cb(sub, nil)
sub.ReleaseRPCResources()
c.Unsubscribe(sub, true, false, 1, true)
})
})
}

// GetHTTPSubscription is called from apiHandler on a HTTP GET request. It
// differs from GetSubscription by making an access call separately, and not
// within the subscription, in order to call access with isHTTP set to true.
Expand Down
24 changes: 24 additions & 0 deletions test/00connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"fmt"
"net/http"
"os"
"testing"

"github.com/resgateio/resgate/server"
Expand Down Expand Up @@ -65,3 +66,26 @@ func TestConnect_AllowOrigin_Connects(t *testing.T) {
})
}
}

// Test that the server responds with 400 on request-target being *.
func TestStart_WithAsteriskAsRequestURI_BadRequestStatusResponse(t *testing.T) {
runTest(t, func(s *Session) {
hreq := s.HTTPRequest("GET", "", nil, func(r *http.Request) {
r.RequestURI = "*"
})
hreq.GetResponse(t).AssertStatusCode(t, http.StatusBadRequest)
})
}

// Test that the server starts and stops without error when enabling the HTTP server
func TestStart_WithHTTPServer_NoErrors(t *testing.T) {
ref := os.Getenv("RESGATE_TEST_EXTENDED")
if ref == "" {
t.Skip("no RESGATE_TEST_EXTENDED environment value")
}
runTest(t, func(s *Session) {}, func(cfg *server.Config) {
cfg.NoHTTP = false
cfg.Port = 58080
cfg.MetricsPort = 58090
})
}
38 changes: 22 additions & 16 deletions test/14http_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,29 @@ func TestHTTPGetInvalidURLs(t *testing.T) {
{"/api/test/mådel/action", http.StatusNotFound, reserr.ErrNotFound},
}

for i, l := range tbl {
runNamedTest(t, fmt.Sprintf("#%d", i+1), func(s *Session) {
hreq := s.HTTPRequest("GET", l.URL, nil)
hresp := hreq.
GetResponse(t).
AssertStatusCode(t, l.ExpectedCode)

if l.Expected != nil {
if err, ok := l.Expected.(*reserr.Error); ok {
hresp.AssertError(t, err)
} else if code, ok := l.Expected.(string); ok {
hresp.AssertErrorCode(t, code)
} else {
hresp.AssertBody(t, l.Expected)
encodings := []string{"json", "jsonFlat"}

for _, enc := range encodings {
for i, l := range tbl {
runNamedTest(t, fmt.Sprintf("#%d (%s)", i+1, enc), func(s *Session) {
hreq := s.HTTPRequest("GET", l.URL, nil)
hresp := hreq.
GetResponse(t).
AssertStatusCode(t, l.ExpectedCode)

if l.Expected != nil {
if err, ok := l.Expected.(*reserr.Error); ok {
hresp.AssertError(t, err)
} else if code, ok := l.Expected.(string); ok {
hresp.AssertErrorCode(t, code)
} else {
hresp.AssertBody(t, l.Expected)
}
}
}
})
}, func(c *server.Config) {
c.APIEncoding = enc
})
}
}
}

Expand Down
Loading

0 comments on commit 41d01f4

Please sign in to comment.