This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1509 from candysmurf/cors
SDI-1659: support for CORS header
- Loading branch information
Showing
9 changed files
with
228 additions
and
15 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -22,10 +22,14 @@ limitations under the License. | |
package rest | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/intelsdi-x/snap/pkg/cfgfile" | ||
. "github.com/smartystreets/goconvey/convey" | ||
"github.com/urfave/negroni" | ||
) | ||
|
||
const ( | ||
|
@@ -161,5 +165,102 @@ func TestRestAPIDefaultConfig(t *testing.T) { | |
Convey("RestKey should be empty", func() { | ||
So(cfg.RestKey, ShouldEqual, "") | ||
}) | ||
Convey("Corsd should be empty", func() { | ||
So(cfg.Corsd, ShouldEqual, "") | ||
}) | ||
}) | ||
} | ||
|
||
type mockServer struct { | ||
n *negroni.Negroni | ||
allowedOrigins map[string]bool | ||
} | ||
|
||
func NewMockServer(cfg *Config) (*mockServer, []string, error) { | ||
s := &mockServer{} | ||
origins, err := s.getAllowedOrigins(cfg.Corsd) | ||
|
||
return s, origins, err | ||
} | ||
|
||
func (s *mockServer) getAllowedOrigins(corsd string) ([]string, error) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
var ok bool | ||
err, ok := r.(error) | ||
if !ok { | ||
err = fmt.Errorf("pkg: %v", r) | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
}() | ||
|
||
if corsd == "" { | ||
return []string{}, nil | ||
} | ||
|
||
vo := []string{} | ||
s.allowedOrigins = map[string]bool{} | ||
|
||
os := strings.Split(corsd, ",") | ||
for _, o := range os { | ||
to := strings.TrimSpace(o) | ||
|
||
// Validates origin formation | ||
u, err := url.Parse(to) | ||
|
||
// Checks if scheme or host exists when no error occured. | ||
if err != nil || u.Scheme == "" || u.Host == "" { | ||
restLogger.Errorf("Invalid origin found %s", to) | ||
return []string{}, fmt.Errorf("Invalid origin found: %s.", to) | ||
} | ||
|
||
vo = append(vo, to) | ||
s.allowedOrigins[to] = true | ||
} | ||
return vo, nil | ||
} | ||
|
||
func TestRestAPICorsd(t *testing.T) { | ||
cfg := GetDefaultConfig() | ||
|
||
Convey("Test cors origin list", t, func() { | ||
|
||
Convey("Origins are valid", func() { | ||
cfg.Corsd = "http://127.0.0.1:80, http://example.com" | ||
s, o, err := NewMockServer(cfg) | ||
|
||
So(len(s.allowedOrigins), ShouldEqual, 2) | ||
So(len(o), ShouldEqual, 2) | ||
So(err, ShouldBeNil) | ||
}) | ||
|
||
Convey("Origins have a wrong separator", func() { | ||
cfg.Corsd = "http://127.0.0.1:80; http://example.com" | ||
s, o, err := NewMockServer(cfg) | ||
|
||
So(err, ShouldNotBeNil) | ||
So(len(s.allowedOrigins), ShouldEqual, 0) | ||
So(len(o), ShouldEqual, 0) | ||
}) | ||
|
||
Convey("Origin misses scheme", func() { | ||
cfg.Corsd = "127.0.0.1:80, http://example.com" | ||
s, o, err := NewMockServer(cfg) | ||
|
||
So(err, ShouldNotBeNil) | ||
So(len(s.allowedOrigins), ShouldEqual, 0) | ||
So(len(o), ShouldEqual, 0) | ||
}) | ||
|
||
Convey("Origin is malformed", func() { | ||
cfg.Corsd = "http://127.0.0.1:80, http://snap.io, [email protected]" | ||
s, o, err := NewMockServer(cfg) | ||
|
||
So(err, ShouldNotBeNil) | ||
So(len(s.allowedOrigins), ShouldEqual, 2) | ||
So(len(o), ShouldEqual, 0) | ||
}) | ||
}) | ||
} |
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