Skip to content

Commit

Permalink
Merge pull request #258 from resgateio/feature/gh-257-increase-test-c…
Browse files Browse the repository at this point in the history
…overage

Feature/gh 257 increase test coverage
  • Loading branch information
jirenius authored Jul 3, 2024
2 parents a42f3fd + 41d01f4 commit 01042d4
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 299 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.

84 changes: 33 additions & 51 deletions server/rescache/resourcePattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,56 @@ type ResourcePattern struct {
hasWild bool
}

const (
pwc = '*'
fwc = '>'
btsep = '.'
)

// ParseResourcePattern parses a string as a resource pattern.
// It uses the same wildcard matching as used in NATS
func ParseResourcePattern(pattern string) ResourcePattern {
p := ResourcePattern{
pattern: pattern,
}

plen := len(pattern)
if plen == 0 {
// ParseResourcePattern parses a string as a resource pattern p. It uses the
// same wildcard matching as used in NATS.
func ParseResourcePattern(p string) ResourcePattern {
l := len(p)
if l == 0 || p[l-1] == '.' {
return ResourcePattern{}
}

var c byte
tcount := 0
offset := 0
start := true
alone := false
hasWild := false
for i := 0; i <= plen; i++ {
if i == plen {
c = btsep
for i, c := range p {
if c == '.' {
if start {
return ResourcePattern{}
}
alone = false
start = true
} else {
c = pattern[i]
}

switch c {
case btsep:
// Empty tokens are invalid
if offset == i {
if alone || c < 33 || c > 126 || c == '?' {
return ResourcePattern{}
}
if hasWild {
if i-offset > 1 {
switch c {
case '>':
if !start || i < l-1 {
return ResourcePattern{}
}
hasWild = false
}
offset = i + 1
tcount++
case pwc:
p.hasWild = true
hasWild = true
case fwc:
// If wildcard isn't the last char
if i < plen-1 {
return ResourcePattern{}
hasWild = true
case '*':
if !start {
return ResourcePattern{}
}
hasWild = true
alone = true
}
p.hasWild = true
hasWild = true
start = false
}
}

return p
return ResourcePattern{pattern: p, hasWild: hasWild}
}

// IsValid reports whether the resource pattern is valid
func (p ResourcePattern) IsValid() bool {
return len(p.pattern) > 0
}

// Match reports whether a resource name, s, matches the resource pattern
// Match reports whether a resource name, s, matches the resource pattern.
func (p ResourcePattern) Match(s string) bool {
if len(p.pattern) == 0 {
plen := len(p.pattern)
if plen == 0 {
return false
}

Expand All @@ -81,7 +64,6 @@ func (p ResourcePattern) Match(s string) bool {
}

slen := len(s)
plen := len(p.pattern)

if plen > slen {
return false
Expand All @@ -91,12 +73,12 @@ func (p ResourcePattern) Match(s string) bool {
pi := 0
for {
switch p.pattern[pi] {
case fwc:
case '>':
return true
case pwc:
case '*':
pi++
for {
if s[si] == btsep {
if s[si] == '.' {
break
}
si++
Expand Down
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
})
}
Loading

0 comments on commit 01042d4

Please sign in to comment.