diff --git a/integration_tests/kvstore/fastly.toml b/integration_tests/kvstore/fastly.toml index c2fd7e6..b44d13f 100644 --- a/integration_tests/kvstore/fastly.toml +++ b/integration_tests/kvstore/fastly.toml @@ -9,6 +9,7 @@ name = "kvstore" service_id = "" [local_server] +backends.httpme.url = "https://http-me.fastly.dev" [local_server.kv_stores] diff --git a/integration_tests/kvstore/main_test.go b/integration_tests/kvstore/main_test.go index 586d312..40a62e4 100644 --- a/integration_tests/kvstore/main_test.go +++ b/integration_tests/kvstore/main_test.go @@ -5,12 +5,14 @@ package main import ( + "context" "maps" "sort" "strconv" "strings" "testing" + "github.com/fastly/compute-sdk-go/fsthttp" "github.com/fastly/compute-sdk-go/kvstore" ) @@ -102,6 +104,30 @@ func TestKVStore(t *testing.T) { if !maps.Equal(wantListKeys, gotListKeys) { t.Errorf("Expected got/want keys mismatch: want=%v, got=%v", mapKeys(wantListKeys), mapKeys(gotListKeys)) } + + uri := "https://http-me.fastly.dev/echo/?body=hello,+world" + req, err := fsthttp.NewRequest("GET", uri, nil) + if err != nil { + t.Errorf("error during NewRequest: uri=%v err=%v", uri, err) + return + } + + ctx := context.Background() + resp, err := req.Send(ctx, "httpme") + + if err := store.Insert("hello", resp.Body); err != nil { + t.Errorf("error during HTTPBody Insert: err=%v", err) + } + + hello, err = store.Lookup("hello") + if err != nil { + t.Errorf("error during HTTPBody Lookup: err=%v", err) + return + } + + if got, want := hello.String(), "hello, world"; got != want { + t.Errorf("HTTPBody Lookup: got %q, want %q", got, want) + } } func mapKeys(m map[string]bool) []string { diff --git a/internal/abi/fastly/kvstore_guest.go b/internal/abi/fastly/kvstore_guest.go index f59fb99..1033bfb 100644 --- a/internal/abi/fastly/kvstore_guest.go +++ b/internal/abi/fastly/kvstore_guest.go @@ -5,8 +5,6 @@ package fastly import ( - "io" - "github.com/fastly/compute-sdk-go/internal/abi/prim" ) @@ -186,16 +184,7 @@ func fastlyKVStoreInsert( ) FastlyStatus // Insert returns a handle to a pending key/value pair insertion. -func (k *KVStore) Insert(key string, value io.Reader, config *KVInsertConfig) (kvstoreInsertHandle, error) { - body, err := NewHTTPBody() - if err != nil { - return 0, err - } - - if _, err := io.Copy(body, value); err != nil { - return 0, err - } - +func (k *KVStore) Insert(key string, body *HTTPBody, config *KVInsertConfig) (kvstoreInsertHandle, error) { if config == nil { config = &KVInsertConfig{} } diff --git a/kvstore/kvstore.go b/kvstore/kvstore.go index cf24cb2..acc2b9d 100644 --- a/kvstore/kvstore.go +++ b/kvstore/kvstore.go @@ -173,7 +173,19 @@ func (s *Store) InsertWithConfig(key string, value io.Reader, config *InsertConf } } - h, err := s.kvstore.Insert(key, value, &abiConf) + var body *fastly.HTTPBody + if abiBody, ok := value.(*fastly.HTTPBody); ok { + body = abiBody + } else { + var err error + body, err = fastly.NewHTTPBody() + if err != nil { + return err + } + io.Copy(body, value) + } + + h, err := s.kvstore.Insert(key, body, &abiConf) if err != nil { return mapFastlyErr(err) }