2020-01-08 19:19:18 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2020-02-06 14:04:29 +00:00
|
|
|
"net/http"
|
2020-01-08 19:19:18 +00:00
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/pkg/testttp"
|
2020-01-08 19:19:18 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_normalizePath(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
path string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "1",
|
|
|
|
path: path.Join("/api/v2/organizations", influxdb.ID(2).String()),
|
|
|
|
expected: "/api/v2/organizations/:id",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2",
|
|
|
|
path: "/api/v2/organizations",
|
|
|
|
expected: "/api/v2/organizations",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "3",
|
|
|
|
path: "/",
|
|
|
|
expected: "/",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "4",
|
|
|
|
path: path.Join("/api/v2/organizations", influxdb.ID(2).String(), "users", influxdb.ID(3).String()),
|
|
|
|
expected: "/api/v2/organizations/:id/users/:id",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
actual := normalizePath(tt.path)
|
|
|
|
assert.Equal(t, tt.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 14:04:29 +00:00
|
|
|
|
|
|
|
func TestCors(t *testing.T) {
|
|
|
|
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte("nextHandler"))
|
|
|
|
})
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
method string
|
|
|
|
headers []string
|
|
|
|
expectedStatus int
|
|
|
|
expectedHeaders map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OPTIONS without Origin",
|
|
|
|
method: "OPTIONS",
|
|
|
|
expectedStatus: http.StatusMethodNotAllowed,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OPTIONS with Origin",
|
|
|
|
method: "OPTIONS",
|
|
|
|
headers: []string{"Origin", "http://myapp.com"},
|
2020-04-07 05:27:02 +00:00
|
|
|
expectedStatus: http.StatusNoContent,
|
2020-02-06 14:04:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "GET with Origin",
|
|
|
|
method: "GET",
|
|
|
|
headers: []string{"Origin", "http://anotherapp.com"},
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedHeaders: map[string]string{
|
2020-04-07 05:27:02 +00:00
|
|
|
"Access-Control-Allow-Origin": "http://anotherapp.com",
|
2020-02-06 14:04:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
svr := SkipOptions(SetCORS(nextHandler))
|
|
|
|
|
|
|
|
testttp.
|
|
|
|
HTTP(t, tt.method, "/", nil).
|
|
|
|
Headers("", "", tt.headers...).
|
|
|
|
Do(svr).
|
|
|
|
ExpectStatus(tt.expectedStatus).
|
|
|
|
ExpectHeaders(tt.expectedHeaders)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|