Jon Perritt | f81e17a | 2014-09-15 01:29:41 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "io/ioutil" |
| 7 | "net/http" |
| 8 | "reflect" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestExtractObjectMetadata(t *testing.T) { |
| 13 | getResult := &http.Response{} |
| 14 | |
| 15 | expected := map[string]string{} |
| 16 | |
| 17 | actual := ExtractMetadata(getResult) |
| 18 | |
| 19 | if !reflect.DeepEqual(expected, actual) { |
| 20 | t.Errorf("Expected: %+v\nActual:%+v", expected, actual) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestExtractContent(t *testing.T) { |
| 25 | responseBody := "'Twas brillig, and the slithy toves" |
| 26 | downloadResult := &http.Response{ |
| 27 | Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)), |
| 28 | } |
| 29 | expected := []byte("'Twas brillig, and the slithy toves") |
| 30 | actual, err := ExtractContent(downloadResult) |
| 31 | if err != nil { |
| 32 | t.Errorf("Error extracting object content: %s", err) |
| 33 | } |
| 34 | if !reflect.DeepEqual(actual, expected) { |
| 35 | t.Errorf("Expected: %+v\nActual:%+v", expected, actual) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestExtractObjectInfo(t *testing.T) { |
| 40 | responseBody := ` |
| 41 | [ |
| 42 | { |
| 43 | "hash": "451e372e48e0f6b1114fa0724aa79fa1", |
| 44 | "last_modified": "2014-01-15T16:41:49.390270", |
| 45 | "bytes": 14, |
| 46 | "name": "goodbye", |
| 47 | "content_type": "application/octet-stream" |
| 48 | }, |
| 49 | { |
| 50 | "hash": "ed076287532e86365e841e92bfc50d8c", |
| 51 | "last_modified": "2014-01-15T16:37:43.427570", |
| 52 | "bytes": 12, |
| 53 | "name": "helloworld", |
| 54 | "content_type": "application/octet-stream" |
| 55 | } |
| 56 | ] |
| 57 | ` |
| 58 | |
| 59 | listResult := &http.Response{ |
| 60 | Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)), |
| 61 | } |
| 62 | |
| 63 | var expected []Object |
| 64 | err := json.Unmarshal([]byte(responseBody), &expected) |
| 65 | if err != nil { |
| 66 | t.Errorf("Error unmarshaling JSON: %s", err) |
| 67 | } |
| 68 | |
| 69 | actual, err := ExtractInfo(listResult) |
| 70 | if err != nil { |
| 71 | t.Errorf("Error extracting objects info: %s", err) |
| 72 | } |
| 73 | |
| 74 | if !reflect.DeepEqual(expected, actual) { |
| 75 | t.Errorf("Expected: %+v\nActual: %+v", expected, actual) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestExtractObjectNames(t *testing.T) { |
| 80 | responseBody := "goodbye\nhelloworld\n" |
| 81 | |
| 82 | listResult := &http.Response{ |
| 83 | Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)), |
| 84 | } |
| 85 | |
| 86 | expected := []string{"goodbye", "helloworld"} |
| 87 | |
| 88 | actual, err := ExtractNames(listResult) |
| 89 | if err != nil { |
| 90 | t.Errorf("Error extracting object names: %s", err) |
| 91 | } |
| 92 | |
| 93 | if !reflect.DeepEqual(expected, actual) { |
| 94 | t.Errorf("Expected: %+v\nActual:%+v", expected, actual) |
| 95 | } |
| 96 | } |