Skip to content

Instantly share code, notes, and snippets.

@fachryansyah
Created December 26, 2019 09:08
Show Gist options
  • Save fachryansyah/7eced0d0a419e4d4173e5a74f5a2e0d7 to your computer and use it in GitHub Desktop.
Save fachryansyah/7eced0d0a419e4d4173e5a74f5a2e0d7 to your computer and use it in GitHub Desktop.
func TestFileUpload(t *testing.T) {
url := fmt.Sprintf("http://localhost%s/api/file-upload", HTTP_PORT_TEST)
var fileSize int = 1024
sampleFile, err := createFileWithFormat(fileSize, ".jpeg")
if err != nil {
t.Fatal(err)
}
defer os.Remove(sampleFile)
log.Println("=========[UPLOAD FILE]========")
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
// crete form for file
fileWriter, err := bodyWriter.CreateFormFile("ktp", sampleFile)
if err != nil {
fmt.Println("error writing to buffer")
t.Fatal(err)
}
// open file handle
fh, err := os.Open(sampleFile)
if err != nil {
fmt.Println("error opening file")
t.Fatal(err)
}
defer fh.Close()
//iocopy
_, err = io.Copy(fileWriter, fh)
if err != nil {
t.Fatal(err)
}
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
resp, err := http.Post(url, contentType, bodyBuf)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var resJSON map[string]interface{}
json.Unmarshal([]byte(respBody), &resJSON)
client, _ := toldata.NewBus(ctx, toldata.ServiceConfiguration{URL: os.Getenv("NATS_URL")})
defer client.Close()
svc := cdl_blob.NewBlobToldataClient(client)
uuid := resJSON["Uuid"].(string)
// blob, err := blobAPI.GetChunks(ctx, &cdl_blob.GetChunksRequest{BlobId: uuid})
// if err != nil && err.Error() == "blob-not-found" {
// log.Println(err)
// }
fn := fmt.Sprintf("%s.jpeg", randName())
fd, err := os.Create(fn)
if err != nil {
t.Fatal(err)
}
defer os.Remove(fn)
fmt.Println(uuid)
err = getChunks(ctx, uuid, fd, svc)
if err != nil {
t.Log(err)
}
// hashing fh
hashFileA := getHash(sampleFile)
hashFileB := getHash(fn)
fmt.Println(hashFileA)
fmt.Println(hashFileB)
fmt.Println(resp.Status)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment