github.com/orangenpresse/up@v0.6.0/http/robots/robots_test.go (about)

     1  package robots
     2  
     3  import (
     4  	"net/http/httptest"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/apex/up"
     9  	"github.com/tj/assert"
    10  
    11  	"github.com/apex/up/config"
    12  	"github.com/apex/up/http/static"
    13  )
    14  
    15  func TestRobots(t *testing.T) {
    16  	c := &up.Config{
    17  		Static: config.Static{
    18  			Dir: "testdata",
    19  		},
    20  	}
    21  
    22  	t.Run("should set X-Robots-Tag", func(t *testing.T) {
    23  		h := New(c, static.New(c))
    24  
    25  		res := httptest.NewRecorder()
    26  		req := httptest.NewRequest("GET", "/", nil)
    27  
    28  		h.ServeHTTP(res, req)
    29  
    30  		assert.Equal(t, 200, res.Code)
    31  		assert.Equal(t, "none", res.Header().Get("X-Robots-Tag"))
    32  		assert.Equal(t, "text/html; charset=utf-8", res.Header().Get("Content-Type"))
    33  		assert.Equal(t, "Index HTML\n", res.Body.String())
    34  	})
    35  
    36  	t.Run("should not set X-Robots-Tag for production stage", func(t *testing.T) {
    37  		os.Setenv("UP_STAGE", "production")
    38  		defer os.Setenv("UP_STAGE", "")
    39  
    40  		h := New(c, static.New(c))
    41  
    42  		res := httptest.NewRecorder()
    43  		req := httptest.NewRequest("GET", "/", nil)
    44  
    45  		h.ServeHTTP(res, req)
    46  
    47  		assert.Equal(t, 200, res.Code)
    48  		assert.Equal(t, "", res.Header().Get("X-Robots-Tag"))
    49  		assert.Equal(t, "text/html; charset=utf-8", res.Header().Get("Content-Type"))
    50  		assert.Equal(t, "Index HTML\n", res.Body.String())
    51  	})
    52  }