Skip to content

Commit

Permalink
Green tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Sep 26, 2014
1 parent 5c9d44f commit 7073f7f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 82 deletions.
49 changes: 1 addition & 48 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func initTestAPI() *api {
a := NewAPI(g)
a.start = func(m *api) {}
a.Start()
a.SetDebug()
a.Debug()

g.AddRobot(gobot.NewTestRobot("Robot1"))
g.AddRobot(gobot.NewTestRobot("Robot2"))
Expand All @@ -31,53 +31,6 @@ func initTestAPI() *api {
return a
}

func TestBasicAuth(t *testing.T) {
a := initTestAPI()

a.SetBasicAuth("admin", "password")

request, _ := http.NewRequest("GET", "/api/", nil)
request.SetBasicAuth("admin", "password")
response := httptest.NewRecorder()
a.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 200)

request, _ = http.NewRequest("GET", "/api/", nil)
request.SetBasicAuth("admin", "wrongPassword")
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 401)
}

func TestAPIAllowRequestsFrom(t *testing.T) {
api := initTestAPI()
api.AllowRequestsFrom("http://server.com")
gobot.Assert(t, api.cors.AllowOrigins, []string{"http://server.com"})
}

func TestCORS(t *testing.T) {
api := initTestAPI()

// Accepted origin
allowedOrigin := []string{"http://server.com"}
api.AllowRequestsFrom(allowedOrigin[0])

request, _ := http.NewRequest("GET", "/api/", nil)
request.Header.Set("Origin", allowedOrigin[0])
response := httptest.NewRecorder()
api.ServeHTTP(response, request)
gobot.Assert(t, response.Header()["Access-Control-Allow-Origin"], allowedOrigin)

// Not accepted Origin
disallowedOrigin := []string{"http://disallowed.com"}
request, _ = http.NewRequest("GET", "/api/", nil)
request.Header.Set("Origin", disallowedOrigin[0])
response = httptest.NewRecorder()
api.ServeHTTP(response, request)
gobot.Refute(t, response.Header()["Access-Control-Allow-Origin"], disallowedOrigin)
gobot.Refute(t, response.Header()["Access-Control-Allow-Origin"], allowedOrigin)
}

func TestRobeaux(t *testing.T) {
a := initTestAPI()
// html assets
Expand Down
27 changes: 27 additions & 0 deletions api/basic_auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package api

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/hybridgroup/gobot"
)

func TestBasicAuth(t *testing.T) {
a := initTestAPI()

a.AddHandler(BasicAuth("admin", "password"))

request, _ := http.NewRequest("GET", "/api/", nil)
request.SetBasicAuth("admin", "password")
response := httptest.NewRecorder()
a.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 200)

request, _ = http.NewRequest("GET", "/api/", nil)
request.SetBasicAuth("admin", "wrongPassword")
response = httptest.NewRecorder()
a.ServeHTTP(response, request)
gobot.Assert(t, response.Code, 401)
}
69 changes: 35 additions & 34 deletions api/cors_test.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,41 @@
package api

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/hybridgroup/gobot"
)

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:*", "http://localhost:*"}
allowedMethods := []string{"GET", "POST"}
allowedHeaders := []string{"Origin", "Content-Type"}
contentType := "application/json; charset=utf-8"
allowOriginPatterns := []string{"^http://.*server:.*$", "^http://localhost:.*$"}

cors := NewCORS(allowedOrigins)

gobot.Assert(t, cors.AllowOrigins, allowedOrigins)
gobot.Assert(t, cors.AllowMethods, allowedMethods)
gobot.Assert(t, cors.AllowHeaders, allowedHeaders)
gobot.Assert(t, cors.ContentType, contentType)
gobot.Assert(t, cors.allowOriginPatterns, allowOriginPatterns)
}

func TestCORSIsOriginAllowed(t *testing.T) {
cors := NewCORS([]string{"*"})
cors := &CORS{AllowOrigins: []string{"*"}}
cors.generatePatterns()

// 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 = NewCORS([]string{"http://localhost:8000"})
cors = &CORS{AllowOrigins: []string{"http://localhost:8000"}}
cors.generatePatterns()

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 = NewCORS([]string{"http://localhost:*", "http://server.com"})
cors = &CORS{AllowOrigins: []string{"http://localhost:*", "http://server.com"}}
cors.generatePatterns()

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 several origins are accepted within the same domain
cors = NewCORS([]string{"http://*.server.com"})
cors = &CORS{AllowOrigins: []string{"http://*.server.com"}}
cors.generatePatterns()

gobot.Assert(t, cors.isOriginAllowed("http://localhost:8000"), false)
gobot.Assert(t, cors.isOriginAllowed("http://localhost:3001"), false)
Expand All @@ -64,19 +44,40 @@ func TestCORSIsOriginAllowed(t *testing.T) {
}

func TestCORSAllowedHeaders(t *testing.T) {
cors := NewCORS([]string{"*"})

cors.AllowHeaders = []string{"Header1", "Header2"}
cors := &CORS{AllowOrigins: []string{"*"}, AllowHeaders: []string{"Header1", "Header2"}}

gobot.Assert(t, cors.AllowedHeaders(), "Header1,Header2")
}

func TestCORSAllowedMethods(t *testing.T) {
cors := NewCORS([]string{"*"})
cors := &CORS{AllowOrigins: []string{"*"}, AllowMethods: []string{"GET", "POST"}}

gobot.Assert(t, cors.AllowedMethods(), "GET,POST")

cors.AllowMethods = []string{"GET", "POST", "PUT"}

gobot.Assert(t, cors.AllowedMethods(), "GET,POST,PUT")
}

func TestCORS(t *testing.T) {
api := initTestAPI()

// Accepted origin
allowedOrigin := []string{"http://server.com"}
api.AddHandler(AllowRequestsFrom(allowedOrigin[0]))

request, _ := http.NewRequest("GET", "/api/", nil)
request.Header.Set("Origin", allowedOrigin[0])
response := httptest.NewRecorder()
api.ServeHTTP(response, request)
gobot.Assert(t, response.Header()["Access-Control-Allow-Origin"], allowedOrigin)

// Not accepted Origin
disallowedOrigin := []string{"http://disallowed.com"}
request, _ = http.NewRequest("GET", "/api/", nil)
request.Header.Set("Origin", disallowedOrigin[0])
response = httptest.NewRecorder()
api.ServeHTTP(response, request)
gobot.Refute(t, response.Header()["Access-Control-Allow-Origin"], disallowedOrigin)
gobot.Refute(t, response.Header()["Access-Control-Allow-Origin"], allowedOrigin)
}

0 comments on commit 7073f7f

Please sign in to comment.