...

Source file src/github.com/gin-gonic/contrib/cors/cors_test.go

Documentation: github.com/gin-gonic/contrib/cors

     1  package cors
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"testing"
     7  
     8  	"github.com/gin-gonic/gin"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
    13  	req, _ := http.NewRequest(method, path, nil)
    14  	w := httptest.NewRecorder()
    15  	r.ServeHTTP(w, req)
    16  	return w
    17  }
    18  
    19  func TestBadConfig(t *testing.T) {
    20  	assert.Panics(t, func() { New(Config{}) })
    21  	assert.Panics(t, func() {
    22  		New(Config{
    23  			AllowAllOrigins: true,
    24  			AllowedOrigins:  []string{"http://google.com"},
    25  		})
    26  	})
    27  	assert.Panics(t, func() {
    28  		New(Config{
    29  			AllowAllOrigins: true,
    30  			AllowOriginFunc: func(origin string) bool { return false },
    31  		})
    32  	})
    33  	assert.Panics(t, func() {
    34  		New(Config{
    35  			AllowedOrigins:  []string{"http://google.com"},
    36  			AllowOriginFunc: func(origin string) bool { return false },
    37  		})
    38  	})
    39  	assert.Panics(t, func() {
    40  		New(Config{
    41  			AllowedOrigins: []string{"google.com"},
    42  		})
    43  	})
    44  }
    45  
    46  func TestDeny0(t *testing.T) {
    47  	called := false
    48  
    49  	router := gin.Default()
    50  	router.Use(New(Config{
    51  		AllowedOrigins: []string{"http://example.com"},
    52  	}))
    53  	router.GET("/", func(c *gin.Context) {
    54  		called = true
    55  	})
    56  
    57  	w := httptest.NewRecorder()
    58  	req, _ := http.NewRequest("GET", "/", nil)
    59  	req.Header.Set("Origin", "https://example.com")
    60  	router.ServeHTTP(w, req)
    61  
    62  	assert.True(t, called)
    63  	assert.NotContains(t, w.Header(), "Access-Control")
    64  }
    65  
    66  func TestDenyAbortOnError(t *testing.T) {
    67  	called := false
    68  
    69  	router := gin.Default()
    70  	router.Use(New(Config{
    71  		AbortOnError:   true,
    72  		AllowedOrigins: []string{"http://example.com"},
    73  	}))
    74  	router.GET("/", func(c *gin.Context) {
    75  		called = true
    76  	})
    77  
    78  	w := httptest.NewRecorder()
    79  	req, _ := http.NewRequest("GET", "/", nil)
    80  	req.Header.Set("Origin", "https://example.com")
    81  	router.ServeHTTP(w, req)
    82  
    83  	assert.False(t, called)
    84  	assert.NotContains(t, w.Header(), "Access-Control")
    85  }
    86  
    87  func TestDeny2(t *testing.T) {
    88  
    89  }
    90  func TestDeny3(t *testing.T) {
    91  
    92  }
    93  
    94  func TestPasses0(t *testing.T) {
    95  
    96  }
    97  
    98  func TestPasses1(t *testing.T) {
    99  
   100  }
   101  
   102  func TestPasses2(t *testing.T) {
   103  
   104  }
   105  

View as plain text