func main() { // contact.Save("Hello","1234","*****@*****.**") c := contact.Contact{ Tel: "123456", Email: "[email protected]", } contact.Add("hello", c) fmt.Println(contact.Get("hello")) }
func saveContact(w http.ResponseWriter, r *http.Request) { var res map[string]string json.NewDecoder(r.Body).Decode(&res) // fmt.Println(res) contact.Add(res["name"],contact.Contact{ Tel : res["tel"], Email : res["mail"], }) fmt.Println(contact.Get(res["name"])) }
func TestGetContact(t *testing.T) { contact.Add("kenshero", contact.Contact{ Tel: "123456", Email: "*****@*****.**", }) ts := httptest.NewServer(http.HandlerFunc(getContact)) defer ts.Close() resp, _ := http.Get(ts.URL + "/contacts/get?name=kenshero") b, _ := ioutil.ReadAll(resp.Body) content := string(b) expectContent := `{"tel":"123456","mail":"*****@*****.**"}` + "\n" if content != expectContent { t.Error("Expect %s but got %s", expectContent, content) } }
func TestPostContact(t *testing.T) { contact.Add("kenshero", contact.Contact{ Tel: "123456", Email: "*****@*****.**", }) ts := httptest.NewServer(http.HandlerFunc(getContact)) defer ts.Close() resp, _ := http.POST(ts.URL+"/contacts/get?name=kenshero", "application/json", nil) if resp.StatusCode != http.StatusMethodNotAllowed { t.Error("Expect 405 but got %d", resp.StatusCode) } expectContent := `{"tel":"123456","mail":"*****@*****.**"}` + "\n" if content != expectContent { t.Error("Expect %s but got %s", expectContent, content) } }