github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/net/http/upload/up2ds/upload_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"net/http"
     8  	"os"
     9  	"path"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/pbberlin/tools/net/http/upload"
    14  )
    15  
    16  func TestUpload(t *testing.T) {
    17  
    18  	log.SetFlags(log.Lshortfile)
    19  
    20  	files := []string{"test.zip", "test.jpg"}
    21  
    22  	for _, v := range files {
    23  
    24  		curdir, _ := os.Getwd()
    25  		filePath := path.Join(curdir, v)
    26  
    27  		extraParams := map[string]string{
    28  			"getparam1":   "val1",
    29  			"mountname":   "mnt02",
    30  			"description": "A zip file - containing dirs and files",
    31  		}
    32  
    33  		urlUp := "https://google.com/upload"
    34  		urlUp = "http://localhost:8085" + upload.UrlUploadReceive
    35  		urlUp = "https://libertarian-islands.appspot.com" + upload.UrlUploadReceive
    36  
    37  		request, err := upload.CreateFilePostRequest(
    38  			urlUp, "filefield", filePath, extraParams)
    39  		if err != nil {
    40  			log.Fatal(err)
    41  		}
    42  
    43  		client := &http.Client{}
    44  		log.Printf("Sending req ... %v", urlUp)
    45  		resp, err := client.Do(request)
    46  
    47  		if err != nil {
    48  			log.Fatal(err)
    49  		} else {
    50  			bts, err := ioutil.ReadAll(resp.Body)
    51  			if err != nil {
    52  				log.Fatal(err)
    53  			}
    54  			defer resp.Body.Close()
    55  			for k, v := range resp.Header {
    56  				fmt.Println("\tHdr: ", k, v)
    57  			}
    58  			fmt.Printf("status: %v\n", resp.StatusCode)
    59  
    60  			bod := string(bts)
    61  			bods := strings.Split(bod, "<span class='body'></span>")
    62  			if len(bods) == 3 {
    63  				bod = bods[1]
    64  			}
    65  
    66  			fmt.Printf("body:   %s\n", bod)
    67  		}
    68  
    69  	}
    70  }