|
1 | 1 | package lars |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/xml" |
4 | 5 | "io" |
5 | 6 | "net/http" |
6 | 7 | "net/http/httptest" |
@@ -501,3 +502,116 @@ func TestAcceptedLanguages(t *testing.T) { |
501 | 502 |
|
502 | 503 | Equal(t, languages, []string{}) |
503 | 504 | } |
| 505 | + |
| 506 | +type zombie struct { |
| 507 | + ID int `json:"id" xml:"id"` |
| 508 | + Name string `json:"name" xml:"name"` |
| 509 | +} |
| 510 | + |
| 511 | +func TestXML(t *testing.T) { |
| 512 | + xmlData := `<zombie><id>1</id><name>Patient Zero</name></zombie>` |
| 513 | + |
| 514 | + l := New() |
| 515 | + l.Get("/xml", func(c Context) { |
| 516 | + c.XML(http.StatusOK, zombie{1, "Patient Zero"}) |
| 517 | + }) |
| 518 | + l.Get("/badxml", func(c Context) { |
| 519 | + if err := c.XML(http.StatusOK, func() {}); err != nil { |
| 520 | + http.Error(c.Response(), err.Error(), http.StatusInternalServerError) |
| 521 | + } |
| 522 | + }) |
| 523 | + |
| 524 | + hf := l.Serve() |
| 525 | + |
| 526 | + r, _ := http.NewRequest(GET, "/xml", nil) |
| 527 | + w := httptest.NewRecorder() |
| 528 | + hf.ServeHTTP(w, r) |
| 529 | + |
| 530 | + Equal(t, w.Code, http.StatusOK) |
| 531 | + Equal(t, w.Header().Get(ContentType), ApplicationXMLCharsetUTF8) |
| 532 | + Equal(t, w.Body.String(), xml.Header+xmlData) |
| 533 | + |
| 534 | + r, _ = http.NewRequest(GET, "/badxml", nil) |
| 535 | + w = httptest.NewRecorder() |
| 536 | + hf.ServeHTTP(w, r) |
| 537 | + |
| 538 | + Equal(t, w.Code, http.StatusInternalServerError) |
| 539 | + Equal(t, w.Header().Get(ContentType), TextPlainCharsetUTF8) |
| 540 | + Equal(t, w.Body.String(), "xml: unsupported type: func()\n") |
| 541 | +} |
| 542 | + |
| 543 | +func TestJSON(t *testing.T) { |
| 544 | + jsonData := `{"id":1,"name":"Patient Zero"}` |
| 545 | + callbackFunc := "CallbackFunc" |
| 546 | + |
| 547 | + l := New() |
| 548 | + l.Get("/json", func(c Context) { |
| 549 | + c.JSON(http.StatusOK, zombie{1, "Patient Zero"}) |
| 550 | + }) |
| 551 | + l.Get("/badjson", func(c Context) { |
| 552 | + if err := c.JSON(http.StatusOK, func() {}); err != nil { |
| 553 | + http.Error(c.Response(), err.Error(), http.StatusInternalServerError) |
| 554 | + } |
| 555 | + }) |
| 556 | + l.Get("/jsonp", func(c Context) { |
| 557 | + c.JSONP(http.StatusOK, zombie{1, "Patient Zero"}, callbackFunc) |
| 558 | + }) |
| 559 | + l.Get("/badjsonp", func(c Context) { |
| 560 | + if err := c.JSONP(http.StatusOK, func() {}, callbackFunc); err != nil { |
| 561 | + http.Error(c.Response(), err.Error(), http.StatusInternalServerError) |
| 562 | + } |
| 563 | + }) |
| 564 | + |
| 565 | + hf := l.Serve() |
| 566 | + |
| 567 | + r, _ := http.NewRequest(GET, "/json", nil) |
| 568 | + w := httptest.NewRecorder() |
| 569 | + hf.ServeHTTP(w, r) |
| 570 | + |
| 571 | + Equal(t, w.Code, http.StatusOK) |
| 572 | + Equal(t, w.Header().Get(ContentType), ApplicationJSONCharsetUTF8) |
| 573 | + Equal(t, w.Body.String(), jsonData) |
| 574 | + |
| 575 | + r, _ = http.NewRequest(GET, "/badjson", nil) |
| 576 | + w = httptest.NewRecorder() |
| 577 | + hf.ServeHTTP(w, r) |
| 578 | + |
| 579 | + Equal(t, w.Code, http.StatusInternalServerError) |
| 580 | + Equal(t, w.Header().Get(ContentType), TextPlainCharsetUTF8) |
| 581 | + Equal(t, w.Body.String(), "json: unsupported type: func()\n") |
| 582 | + |
| 583 | + r, _ = http.NewRequest(GET, "/jsonp", nil) |
| 584 | + w = httptest.NewRecorder() |
| 585 | + hf.ServeHTTP(w, r) |
| 586 | + |
| 587 | + Equal(t, w.Code, http.StatusOK) |
| 588 | + Equal(t, w.Header().Get(ContentType), ApplicationJavaScriptCharsetUTF8) |
| 589 | + Equal(t, w.Body.String(), callbackFunc+"("+jsonData+");") |
| 590 | + |
| 591 | + r, _ = http.NewRequest(GET, "/badjsonp", nil) |
| 592 | + w = httptest.NewRecorder() |
| 593 | + hf.ServeHTTP(w, r) |
| 594 | + |
| 595 | + Equal(t, w.Code, http.StatusInternalServerError) |
| 596 | + Equal(t, w.Header().Get(ContentType), TextPlainCharsetUTF8) |
| 597 | + Equal(t, w.Body.String(), "json: unsupported type: func()\n") |
| 598 | +} |
| 599 | + |
| 600 | +func TestText(t *testing.T) { |
| 601 | + txtData := `OMG I'm infected! #zombie` |
| 602 | + |
| 603 | + l := New() |
| 604 | + l.Get("/text", func(c Context) { |
| 605 | + c.Text(http.StatusOK, txtData) |
| 606 | + }) |
| 607 | + |
| 608 | + hf := l.Serve() |
| 609 | + |
| 610 | + r, _ := http.NewRequest(GET, "/text", nil) |
| 611 | + w := httptest.NewRecorder() |
| 612 | + hf.ServeHTTP(w, r) |
| 613 | + |
| 614 | + Equal(t, w.Code, http.StatusOK) |
| 615 | + Equal(t, w.Header().Get(ContentType), TextPlainCharsetUTF8) |
| 616 | + Equal(t, w.Body.String(), txtData) |
| 617 | +} |
0 commit comments