github.com/aavshr/aws-sdk-go@v1.41.3/aws/corehandlers/handlers_1_10_test.go (about)

     1  //go:build go1.10
     2  // +build go1.10
     3  
     4  package corehandlers_test
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  	"net/http"
    10  	"net/http/httptest"
    11  	"testing"
    12  
    13  	"github.com/aavshr/aws-sdk-go/aws"
    14  	"github.com/aavshr/aws-sdk-go/aws/credentials"
    15  	"github.com/aavshr/aws-sdk-go/aws/request"
    16  	"github.com/aavshr/aws-sdk-go/awstesting/unit"
    17  	"github.com/aavshr/aws-sdk-go/service/s3"
    18  )
    19  
    20  func TestSendHandler_HEADNoBody(t *testing.T) {
    21  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    22  		defer r.Body.Close()
    23  		if e, a := "HEAD", r.Method; e != a {
    24  			t.Errorf("expected %v method, got %v", e, a)
    25  		}
    26  		var buf bytes.Buffer
    27  		io.Copy(&buf, r.Body)
    28  
    29  		if n := buf.Len(); n != 0 {
    30  			t.Errorf("expect empty body, got %d", n)
    31  		}
    32  
    33  		w.WriteHeader(http.StatusOK)
    34  	}))
    35  	defer server.Close()
    36  
    37  	svc := s3.New(unit.Session, &aws.Config{
    38  		Endpoint:         aws.String(server.URL),
    39  		Credentials:      credentials.AnonymousCredentials,
    40  		S3ForcePathStyle: aws.Bool(true),
    41  		DisableSSL:       aws.Bool(true),
    42  	})
    43  
    44  	req, _ := svc.HeadObjectRequest(&s3.HeadObjectInput{
    45  		Bucket: aws.String("bucketname"),
    46  		Key:    aws.String("keyname"),
    47  	})
    48  
    49  	if e, a := request.NoBody, req.HTTPRequest.Body; e != a {
    50  		t.Fatalf("expect %T request body, got %T", e, a)
    51  	}
    52  
    53  	if err := req.Send(); err != nil {
    54  		t.Fatalf("expect no error, got %v", err)
    55  	}
    56  	if e, a := http.StatusOK, req.HTTPResponse.StatusCode; e != a {
    57  		t.Errorf("expect %d status code, got %d", e, a)
    58  	}
    59  }