123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- package s3api
- import (
- "fmt"
- "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
- "net/http"
- "reflect"
- "sort"
- "strings"
- "testing"
- )
- type H map[string]string
- func (h H) String() string {
- pairs := make([]string, 0, len(h))
- for k, v := range h {
- pairs = append(pairs, fmt.Sprintf("%s : %s", k, v))
- }
- sort.Strings(pairs)
- join := strings.Join(pairs, "\n")
- return "\n" + join + "\n"
- }
- var processMetadataTestCases = []struct {
- caseId int
- request H
- existing H
- getTags H
- want H
- }{
- {
- 201,
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=request",
- },
- H{
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-Type": "existing",
- },
- H{
- "A": "B",
- "a": "b",
- "type": "existing",
- },
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging": "A=B&a=b&type=existing",
- },
- },
- {
- 202,
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=request",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- },
- H{
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-Type": "existing",
- },
- H{
- "A": "B",
- "a": "b",
- "type": "existing",
- },
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=existing",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- },
- },
- {
- 203,
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=request",
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- H{
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-Type": "existing",
- },
- H{
- "A": "B",
- "a": "b",
- "type": "existing",
- },
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging": "A=B&a=b&type=request",
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- },
- {
- 204,
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=request",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- H{
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-a": "b",
- "X-Amz-Tagging-Type": "existing",
- },
- H{
- "A": "B",
- "a": "b",
- "type": "existing",
- },
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=request",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- },
- {
- 205,
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=request",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- H{},
- H{},
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=request",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- },
- {
- 206,
- H{
- "User-Agent": "firefox",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- H{
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-a": "b",
- "X-Amz-Tagging-Type": "existing",
- },
- H{
- "A": "B",
- "a": "b",
- "type": "existing",
- },
- H{
- "User-Agent": "firefox",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- },
- {
- 207,
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- H{
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-a": "b",
- "X-Amz-Tagging-Type": "existing",
- },
- H{
- "A": "B",
- "a": "b",
- "type": "existing",
- },
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- },
- }
- var processMetadataBytesTestCases = []struct {
- caseId int
- request H
- existing H
- want H
- }{
- {
- 101,
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=request",
- },
- H{
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-a": "b",
- "X-Amz-Tagging-type": "existing",
- },
- H{
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-a": "b",
- "X-Amz-Tagging-type": "existing",
- },
- },
- {
- 102,
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=request",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- },
- H{
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-a": "b",
- "X-Amz-Tagging-type": "existing",
- },
- H{
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-a": "b",
- "X-Amz-Tagging-type": "existing",
- },
- },
- {
- 103,
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=request",
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- H{
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-a": "b",
- "X-Amz-Tagging-type": "existing",
- },
- H{
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-a": "b",
- "X-Amz-Tagging-type": "request",
- },
- },
- {
- 104,
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=request",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- H{
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-a": "b",
- "X-Amz-Tagging-type": "existing",
- },
- H{
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-a": "b",
- "X-Amz-Tagging-type": "request",
- },
- },
- {
- 105,
- H{
- "User-Agent": "firefox",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- H{
- "X-Amz-Meta-My-Meta": "existing",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-a": "b",
- "X-Amz-Tagging-type": "existing",
- },
- H{},
- },
- {
- 107,
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=request",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- H{},
- H{
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging-A": "B",
- "X-Amz-Tagging-a": "b",
- "X-Amz-Tagging-type": "request",
- },
- },
- {
- 108,
- H{
- "User-Agent": "firefox",
- "X-Amz-Meta-My-Meta": "request",
- "X-Amz-Tagging": "A=B&a=b&type=request*",
- s3_constants.AmzUserMetaDirective: DirectiveReplace,
- s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
- },
- H{},
- H{},
- },
- }
- func TestProcessMetadata(t *testing.T) {
- for _, tc := range processMetadataTestCases {
- reqHeader := transferHToHeader(tc.request)
- existing := transferHToHeader(tc.existing)
- replaceMeta, replaceTagging := replaceDirective(reqHeader)
- err := processMetadata(reqHeader, existing, replaceMeta, replaceTagging, func(_ string, _ string) (tags map[string]string, err error) {
- return tc.getTags, nil
- }, "", "")
- if err != nil {
- t.Error(err)
- }
- result := transferHeaderToH(reqHeader)
- fmtTagging(result, tc.want)
- if !reflect.DeepEqual(result, tc.want) {
- t.Error(fmt.Errorf("\n### CaseID: %d ###"+
- "\nRequest:%v"+
- "\nExisting:%v"+
- "\nGetTags:%v"+
- "\nWant:%v"+
- "\nActual:%v",
- tc.caseId, tc.request, tc.existing, tc.getTags, tc.want, result))
- }
- }
- }
- func TestProcessMetadataBytes(t *testing.T) {
- for _, tc := range processMetadataBytesTestCases {
- reqHeader := transferHToHeader(tc.request)
- existing := transferHToBytesArr(tc.existing)
- replaceMeta, replaceTagging := replaceDirective(reqHeader)
- extends, _ := processMetadataBytes(reqHeader, existing, replaceMeta, replaceTagging)
- result := transferBytesArrToH(extends)
- fmtTagging(result, tc.want)
- if !reflect.DeepEqual(result, tc.want) {
- t.Error(fmt.Errorf("\n### CaseID: %d ###"+
- "\nRequest:%v"+
- "\nExisting:%v"+
- "\nWant:%v"+
- "\nActual:%v",
- tc.caseId, tc.request, tc.existing, tc.want, result))
- }
- }
- }
- func fmtTagging(maps ...map[string]string) {
- for _, m := range maps {
- if tagging := m[s3_constants.AmzObjectTagging]; len(tagging) > 0 {
- split := strings.Split(tagging, "&")
- sort.Strings(split)
- m[s3_constants.AmzObjectTagging] = strings.Join(split, "&")
- }
- }
- }
- func transferHToHeader(data map[string]string) http.Header {
- header := http.Header{}
- for k, v := range data {
- header.Add(k, v)
- }
- return header
- }
- func transferHToBytesArr(data map[string]string) map[string][]byte {
- m := make(map[string][]byte, len(data))
- for k, v := range data {
- m[k] = []byte(v)
- }
- return m
- }
- func transferBytesArrToH(data map[string][]byte) H {
- m := make(map[string]string, len(data))
- for k, v := range data {
- m[k] = string(v)
- }
- return m
- }
- func transferHeaderToH(data map[string][]string) H {
- m := make(map[string]string, len(data))
- for k, v := range data {
- m[k] = v[len(v)-1]
- }
- return m
- }
|