|
| 1 | +package golax |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func Test_ExtendedWriter_WriteTwice(t *testing.T) { |
| 12 | + world := NewWorld() |
| 13 | + defer world.Destroy() |
| 14 | + |
| 15 | + world.Api.Handler500 = func(c *Context) { |
| 16 | + json.NewEncoder(c.Response).Encode(map[string]interface{}{ |
| 17 | + "status": c.LastError.StatusCode, |
| 18 | + "description": c.LastError.Description, |
| 19 | + }) |
| 20 | + } |
| 21 | + |
| 22 | + world.Api.Root. |
| 23 | + Interceptor(InterceptorError). |
| 24 | + Node("example"). |
| 25 | + Method("GET", func(c *Context) { |
| 26 | + c.Response.WriteHeader(400) |
| 27 | + c.Response.WriteHeader(401) |
| 28 | + |
| 29 | + c.Response.Write([]byte("Hello\n")) |
| 30 | + panic("This is a panic!") |
| 31 | + c.Response.Write([]byte("world")) |
| 32 | + |
| 33 | + }) |
| 34 | + |
| 35 | + fmt.Println("=============================================") |
| 36 | + |
| 37 | + res := world.Request("GET", "/example").Do() |
| 38 | + |
| 39 | + if 400 != res.StatusCode { |
| 40 | + t.Error("Status code should be 400") |
| 41 | + } |
| 42 | + |
| 43 | + body := res.BodyString() |
| 44 | + |
| 45 | + body_lines := strings.Split(body, "\n") |
| 46 | + |
| 47 | + if "Hello" != body_lines[0] { |
| 48 | + t.Error("First line should be `Hello` instead of " + body_lines[0]) |
| 49 | + } |
| 50 | + |
| 51 | + body_json := map[string]interface{}{} |
| 52 | + |
| 53 | + json.Unmarshal([]byte(body_lines[1]), &body_json) |
| 54 | + |
| 55 | + if !reflect.DeepEqual(float64(500), body_json["status"]) { |
| 56 | + t.Error("Body json status should be status:500") |
| 57 | + } |
| 58 | + |
| 59 | + if !strings.HasPrefix(body_json["description"].(string), "This is a panic!") { |
| 60 | + t.Error("Body json description should start by `This is a panic!` ") |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments