forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle allowed CORS requests origins
- Loading branch information
Showing
4 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package api | ||
|
||
type CORS struct { | ||
AllowOrigins []string | ||
AllowHeaders []string // Not yet implemented | ||
AllowMethods []string // ditto | ||
} | ||
|
||
func NewCORS(allowedOrigins []string) *CORS { | ||
return &CORS{ | ||
AllowOrigins: allowedOrigins, | ||
AllowMethods: []string{"GET", "POST"}, | ||
AllowHeaders: []string{"Origin", "Content-Type"}, | ||
} | ||
} | ||
|
||
func (c *CORS) isOriginAllowed(currentOrigin string) bool { | ||
for _, allowedOrigin := range c.AllowOrigins { | ||
if "*" == allowedOrigin || currentOrigin == allowedOrigin { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/hybridgroup/gobot" | ||
"testing" | ||
) | ||
|
||
func TestNewCORS(t *testing.T) { | ||
var cors interface{} = NewCORS([]string{}) | ||
|
||
// Does it return a pointer to an instance of CORS? | ||
_, ok := cors.(*CORS) | ||
if !ok { | ||
t.Errorf("NewCORS() should have returned a *CORS") | ||
} | ||
} | ||
|
||
func TestNewCorsSetsProperties(t *testing.T) { | ||
allowedOrigins := []string{"http://server:port"} | ||
|
||
cors := NewCORS(allowedOrigins) | ||
|
||
gobot.Assert(t, cors.AllowOrigins, allowedOrigins) | ||
} | ||
|
||
func TestCORSIsOriginAllowed(t *testing.T) { | ||
cors := NewCORS([]string{"*"}) | ||
|
||
// When all the origins are accepted | ||
gobot.Assert(t, cors.isOriginAllowed("http://localhost:8000"), true) | ||
gobot.Assert(t, cors.isOriginAllowed("http://localhost:3001"), true) | ||
gobot.Assert(t, cors.isOriginAllowed("http://server.com"), true) | ||
|
||
// When one origin is accepted | ||
cors.AllowOrigins = []string{"http://localhost:8000"} | ||
|
||
gobot.Assert(t, cors.isOriginAllowed("http://localhost:8000"), true) | ||
gobot.Assert(t, cors.isOriginAllowed("http://localhost:3001"), false) | ||
gobot.Assert(t, cors.isOriginAllowed("http://server.com"), false) | ||
|
||
// When several origins are accepted | ||
cors.AllowOrigins = []string{"http://localhost:8000", "http://server.com"} | ||
|
||
gobot.Assert(t, cors.isOriginAllowed("http://localhost:8000"), true) | ||
gobot.Assert(t, cors.isOriginAllowed("http://localhost:3001"), false) | ||
gobot.Assert(t, cors.isOriginAllowed("http://server.com"), true) | ||
} |