1package godo 2 3import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "reflect" 8 "testing" 9) 10 11func TestFloatingIPsActions_Assign(t *testing.T) { 12 setup() 13 defer teardown() 14 dropletID := 12345 15 assignRequest := &ActionRequest{ 16 "droplet_id": float64(dropletID), // encoding/json decodes numbers as floats 17 "type": "assign", 18 } 19 20 mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) { 21 v := new(ActionRequest) 22 err := json.NewDecoder(r.Body).Decode(v) 23 if err != nil { 24 t.Fatalf("decode json: %v", err) 25 } 26 27 testMethod(t, r, http.MethodPost) 28 if !reflect.DeepEqual(v, assignRequest) { 29 t.Errorf("Request body = %#v, expected %#v", v, assignRequest) 30 } 31 32 fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`) 33 34 }) 35 36 assign, _, err := client.FloatingIPActions.Assign(ctx, "192.168.0.1", 12345) 37 if err != nil { 38 t.Errorf("FloatingIPsActions.Assign returned error: %v", err) 39 } 40 41 expected := &Action{Status: "in-progress"} 42 if !reflect.DeepEqual(assign, expected) { 43 t.Errorf("FloatingIPsActions.Assign returned %+v, expected %+v", assign, expected) 44 } 45} 46 47func TestFloatingIPsActions_Unassign(t *testing.T) { 48 setup() 49 defer teardown() 50 51 unassignRequest := &ActionRequest{ 52 "type": "unassign", 53 } 54 55 mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) { 56 v := new(ActionRequest) 57 err := json.NewDecoder(r.Body).Decode(v) 58 if err != nil { 59 t.Fatalf("decode json: %v", err) 60 } 61 62 testMethod(t, r, http.MethodPost) 63 if !reflect.DeepEqual(v, unassignRequest) { 64 t.Errorf("Request body = %+v, expected %+v", v, unassignRequest) 65 } 66 67 fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`) 68 }) 69 70 action, _, err := client.FloatingIPActions.Unassign(ctx, "192.168.0.1") 71 if err != nil { 72 t.Errorf("FloatingIPsActions.Get returned error: %v", err) 73 } 74 75 expected := &Action{Status: "in-progress"} 76 if !reflect.DeepEqual(action, expected) { 77 t.Errorf("FloatingIPsActions.Get returned %+v, expected %+v", action, expected) 78 } 79} 80 81func TestFloatingIPsActions_Get(t *testing.T) { 82 setup() 83 defer teardown() 84 85 mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions/456", func(w http.ResponseWriter, r *http.Request) { 86 testMethod(t, r, http.MethodGet) 87 fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`) 88 }) 89 90 action, _, err := client.FloatingIPActions.Get(ctx, "192.168.0.1", 456) 91 if err != nil { 92 t.Errorf("FloatingIPsActions.Get returned error: %v", err) 93 } 94 95 expected := &Action{Status: "in-progress"} 96 if !reflect.DeepEqual(action, expected) { 97 t.Errorf("FloatingIPsActions.Get returned %+v, expected %+v", action, expected) 98 } 99} 100 101func TestFloatingIPsActions_List(t *testing.T) { 102 setup() 103 defer teardown() 104 105 mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) { 106 testMethod(t, r, http.MethodGet) 107 fmt.Fprintf(w, `{"actions":[{"status":"in-progress"}]}`) 108 }) 109 110 actions, _, err := client.FloatingIPActions.List(ctx, "192.168.0.1", nil) 111 if err != nil { 112 t.Errorf("FloatingIPsActions.List returned error: %v", err) 113 } 114 115 expected := []Action{{Status: "in-progress"}} 116 if !reflect.DeepEqual(actions, expected) { 117 t.Errorf("FloatingIPsActions.List returned %+v, expected %+v", actions, expected) 118 } 119} 120 121func TestFloatingIPsActions_ListMultiplePages(t *testing.T) { 122 setup() 123 defer teardown() 124 125 mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) { 126 testMethod(t, r, http.MethodGet) 127 fmt.Fprint(w, `{"actions":[{"status":"in-progress"}], "links":{"pages":{"next":"http://example.com/v2/floating_ips/192.168.0.1/actions?page=2"}}}`) 128 }) 129 130 _, resp, err := client.FloatingIPActions.List(ctx, "192.168.0.1", nil) 131 if err != nil { 132 t.Errorf("FloatingIPsActions.List returned error: %v", err) 133 } 134 135 checkCurrentPage(t, resp, 1) 136} 137 138func TestFloatingIPsActions_ListPageByNumber(t *testing.T) { 139 setup() 140 defer teardown() 141 142 jBlob := ` 143 { 144 "actions":[{"status":"in-progress"}], 145 "links":{ 146 "pages":{ 147 "next":"http://example.com/v2/regions/?page=3", 148 "prev":"http://example.com/v2/regions/?page=1", 149 "last":"http://example.com/v2/regions/?page=3", 150 "first":"http://example.com/v2/regions/?page=1" 151 } 152 } 153 }` 154 155 mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) { 156 testMethod(t, r, http.MethodGet) 157 fmt.Fprint(w, jBlob) 158 }) 159 160 opt := &ListOptions{Page: 2} 161 _, resp, err := client.FloatingIPActions.List(ctx, "192.168.0.1", opt) 162 if err != nil { 163 t.Errorf("FloatingIPsActions.List returned error: %v", err) 164 } 165 166 checkCurrentPage(t, resp, 2) 167} 168