github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/appfs/server_test.go (about)

     1  package appfs
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func Test_serveContent(t *testing.T) {
    13  	t.Run("with other than a HEAD method", func(t *testing.T) {
    14  		r := httptest.NewRequest(http.MethodGet, "http://localhost/foo/bar", nil)
    15  		w := httptest.NewRecorder()
    16  
    17  		serveContent(w, r, "application/json", 10, strings.NewReader("foobar"))
    18  
    19  		assert.Equal(t, http.StatusOK, w.Result().StatusCode)
    20  		assert.Equal(t, "application/json", w.Result().Header.Get("Content-Type"))
    21  		assert.Equal(t, "foobar", w.Body.String())
    22  
    23  		// Not that the length 10 as the `size` parameter and not `len("foobar")`
    24  		assert.Equal(t, "10", w.Result().Header.Get("Content-Length"))
    25  	})
    26  
    27  	t.Run("with the HEAD method", func(t *testing.T) {
    28  		r := httptest.NewRequest(http.MethodHead, "http://localhost/foo/bar", nil)
    29  		w := httptest.NewRecorder()
    30  
    31  		serveContent(w, r, "application/json", 10, strings.NewReader("foobar"))
    32  
    33  		assert.Equal(t, http.StatusOK, w.Result().StatusCode)
    34  		assert.Equal(t, "application/json", w.Result().Header.Get("Content-Type"))
    35  
    36  		// With HEAD we don't setup a body
    37  		assert.Empty(t, w.Body.String())
    38  
    39  		// Not that the length 10 as the `size` parameter and not `len("foobar")`
    40  		assert.Equal(t, "10", w.Result().Header.Get("Content-Length"))
    41  	})
    42  }