Skip to content

Commit

Permalink
add: robots middleware (#627)
Browse files Browse the repository at this point in the history
Closes #610
  • Loading branch information
markelog authored and tj committed Mar 30, 2018
1 parent 7ffbcd3 commit 527bac4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/apex/up/http/poweredby"
"github.com/apex/up/http/redirects"
"github.com/apex/up/http/relay"
"github.com/apex/up/http/robots"
"github.com/apex/up/http/static"
)

Expand All @@ -36,6 +37,7 @@ func FromConfig(c *up.Config) (http.Handler, error) {
// New handler complete with all Up middleware.
func New(c *up.Config, h http.Handler) (http.Handler, error) {
h = poweredby.New("up", h)
h = robots.New(c, h)
h = static.NewDynamic(c, h)

h, err := headers.New(c, h)
Expand Down
2 changes: 2 additions & 0 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestNode(t *testing.T) {

header := make(http.Header)
header.Add("X-Powered-By", "up")
header.Add("X-Robots-Tag", "none")
header.Add("X-Foo", "bar")
header.Add("Content-Length", "11")
header.Add("Content-Type", "text/plain; charset=utf-8")
Expand Down Expand Up @@ -67,6 +68,7 @@ func TestStatic(t *testing.T) {

header := make(http.Header)
header.Add("X-Powered-By", "up")
header.Add("X-Robots-Tag", "none")
header.Add("Content-Length", "12")
header.Add("Content-Type", "text/html; charset=utf-8")
header.Add("Accept-Ranges", "bytes")
Expand Down
21 changes: 21 additions & 0 deletions http/robots/robots.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Package robots provides a way of dealing with robots exclusion protocol
package robots

import (
"net/http"
"os"

"github.com/apex/up"
)

// New robots middleware.
func New(c *up.Config, next http.Handler) http.Handler {
if os.Getenv("UP_STAGE") == "production" {
return next
}

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Robots-Tag", "none")
next.ServeHTTP(w, r)
})
}
52 changes: 52 additions & 0 deletions http/robots/robots_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package robots

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

"github.com/apex/up"
"github.com/tj/assert"

"github.com/apex/up/config"
"github.com/apex/up/http/static"
)

func TestRobots(t *testing.T) {
c := &up.Config{
Static: config.Static{
Dir: "testdata",
},
}

t.Run("should set X-Robots-Tag", func(t *testing.T) {
h := New(c, static.New(c))

res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)

h.ServeHTTP(res, req)

assert.Equal(t, 200, res.Code)
assert.Equal(t, "none", res.Header().Get("X-Robots-Tag"))
assert.Equal(t, "text/html; charset=utf-8", res.Header().Get("Content-Type"))
assert.Equal(t, "Index HTML\n", res.Body.String())
})

t.Run("should not set X-Robots-Tag for production stage", func(t *testing.T) {
os.Setenv("UP_STAGE", "production")
defer os.Setenv("UP_STAGE", "")

h := New(c, static.New(c))

res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)

h.ServeHTTP(res, req)

assert.Equal(t, 200, res.Code)
assert.Equal(t, "", res.Header().Get("X-Robots-Tag"))
assert.Equal(t, "text/html; charset=utf-8", res.Header().Get("Content-Type"))
assert.Equal(t, "Index HTML\n", res.Body.String())
})
}
1 change: 1 addition & 0 deletions http/robots/testdata/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index HTML
3 changes: 3 additions & 0 deletions http/robots/testdata/up.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "app"
}

0 comments on commit 527bac4

Please sign in to comment.