talos/pkg/provision/internal/inmemhttp/inmemhttp_test.go
Andrey Smirnov 96aa9638f7
chore: rename talos-systems/talos to siderolabs/talos
There's a cyclic dependency on siderolink library which imports talos
machinery back. We will fix that after we get talos pushed under a new
name.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
2022-11-03 16:50:32 +04:00

62 lines
1.5 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package inmemhttp_test
import (
"context"
"fmt"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/siderolabs/talos/pkg/provision/internal/inmemhttp"
)
func TestServer(t *testing.T) {
srv, err := inmemhttp.NewServer("localhost:0")
assert.NoError(t, err)
contents := []byte("DEADBEEF")
assert.NoError(t, srv.AddFile("test.txt", contents))
srv.Serve()
defer srv.Shutdown(context.Background()) //nolint:errcheck
resp, err := http.Get(fmt.Sprintf("http://%s/test.txt", srv.GetAddr())) //nolint:noctx
assert.NoError(t, err)
defer resp.Body.Close() //nolint:errcheck
assert.Equal(t, http.StatusOK, resp.StatusCode)
got, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, contents, got)
assert.NoError(t, resp.Body.Close())
resp, err = http.Head(fmt.Sprintf("http://%s/test.txt", srv.GetAddr())) //nolint:noctx
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.EqualValues(t, 8, resp.ContentLength)
assert.NoError(t, resp.Body.Close())
resp, err = http.Get(fmt.Sprintf("http://%s/test.txt2", srv.GetAddr())) //nolint:noctx
assert.NoError(t, err)
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
assert.NoError(t, resp.Body.Close())
assert.NoError(t, srv.Shutdown(context.Background()))
}