github.com/astaxie/beego@v1.12.3/namespace_test.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package beego
    16  
    17  import (
    18  	"net/http"
    19  	"net/http/httptest"
    20  	"strconv"
    21  	"testing"
    22  
    23  	"github.com/astaxie/beego/context"
    24  )
    25  
    26  func TestNamespaceGet(t *testing.T) {
    27  	r, _ := http.NewRequest("GET", "/v1/user", nil)
    28  	w := httptest.NewRecorder()
    29  
    30  	ns := NewNamespace("/v1")
    31  	ns.Get("/user", func(ctx *context.Context) {
    32  		ctx.Output.Body([]byte("v1_user"))
    33  	})
    34  	AddNamespace(ns)
    35  	BeeApp.Handlers.ServeHTTP(w, r)
    36  	if w.Body.String() != "v1_user" {
    37  		t.Errorf("TestNamespaceGet can't run, get the response is " + w.Body.String())
    38  	}
    39  }
    40  
    41  func TestNamespacePost(t *testing.T) {
    42  	r, _ := http.NewRequest("POST", "/v1/user/123", nil)
    43  	w := httptest.NewRecorder()
    44  
    45  	ns := NewNamespace("/v1")
    46  	ns.Post("/user/:id", func(ctx *context.Context) {
    47  		ctx.Output.Body([]byte(ctx.Input.Param(":id")))
    48  	})
    49  	AddNamespace(ns)
    50  	BeeApp.Handlers.ServeHTTP(w, r)
    51  	if w.Body.String() != "123" {
    52  		t.Errorf("TestNamespacePost can't run, get the response is " + w.Body.String())
    53  	}
    54  }
    55  
    56  func TestNamespaceNest(t *testing.T) {
    57  	r, _ := http.NewRequest("GET", "/v1/admin/order", nil)
    58  	w := httptest.NewRecorder()
    59  
    60  	ns := NewNamespace("/v1")
    61  	ns.Namespace(
    62  		NewNamespace("/admin").
    63  			Get("/order", func(ctx *context.Context) {
    64  				ctx.Output.Body([]byte("order"))
    65  			}),
    66  	)
    67  	AddNamespace(ns)
    68  	BeeApp.Handlers.ServeHTTP(w, r)
    69  	if w.Body.String() != "order" {
    70  		t.Errorf("TestNamespaceNest can't run, get the response is " + w.Body.String())
    71  	}
    72  }
    73  
    74  func TestNamespaceNestParam(t *testing.T) {
    75  	r, _ := http.NewRequest("GET", "/v1/admin/order/123", nil)
    76  	w := httptest.NewRecorder()
    77  
    78  	ns := NewNamespace("/v1")
    79  	ns.Namespace(
    80  		NewNamespace("/admin").
    81  			Get("/order/:id", func(ctx *context.Context) {
    82  				ctx.Output.Body([]byte(ctx.Input.Param(":id")))
    83  			}),
    84  	)
    85  	AddNamespace(ns)
    86  	BeeApp.Handlers.ServeHTTP(w, r)
    87  	if w.Body.String() != "123" {
    88  		t.Errorf("TestNamespaceNestParam can't run, get the response is " + w.Body.String())
    89  	}
    90  }
    91  
    92  func TestNamespaceRouter(t *testing.T) {
    93  	r, _ := http.NewRequest("GET", "/v1/api/list", nil)
    94  	w := httptest.NewRecorder()
    95  
    96  	ns := NewNamespace("/v1")
    97  	ns.Router("/api/list", &TestController{}, "*:List")
    98  	AddNamespace(ns)
    99  	BeeApp.Handlers.ServeHTTP(w, r)
   100  	if w.Body.String() != "i am list" {
   101  		t.Errorf("TestNamespaceRouter can't run, get the response is " + w.Body.String())
   102  	}
   103  }
   104  
   105  func TestNamespaceAutoFunc(t *testing.T) {
   106  	r, _ := http.NewRequest("GET", "/v1/test/list", nil)
   107  	w := httptest.NewRecorder()
   108  
   109  	ns := NewNamespace("/v1")
   110  	ns.AutoRouter(&TestController{})
   111  	AddNamespace(ns)
   112  	BeeApp.Handlers.ServeHTTP(w, r)
   113  	if w.Body.String() != "i am list" {
   114  		t.Errorf("user define func can't run")
   115  	}
   116  }
   117  
   118  func TestNamespaceFilter(t *testing.T) {
   119  	r, _ := http.NewRequest("GET", "/v1/user/123", nil)
   120  	w := httptest.NewRecorder()
   121  
   122  	ns := NewNamespace("/v1")
   123  	ns.Filter("before", func(ctx *context.Context) {
   124  		ctx.Output.Body([]byte("this is Filter"))
   125  	}).
   126  		Get("/user/:id", func(ctx *context.Context) {
   127  			ctx.Output.Body([]byte(ctx.Input.Param(":id")))
   128  		})
   129  	AddNamespace(ns)
   130  	BeeApp.Handlers.ServeHTTP(w, r)
   131  	if w.Body.String() != "this is Filter" {
   132  		t.Errorf("TestNamespaceFilter can't run, get the response is " + w.Body.String())
   133  	}
   134  }
   135  
   136  func TestNamespaceCond(t *testing.T) {
   137  	r, _ := http.NewRequest("GET", "/v2/test/list", nil)
   138  	w := httptest.NewRecorder()
   139  
   140  	ns := NewNamespace("/v2")
   141  	ns.Cond(func(ctx *context.Context) bool {
   142  		return ctx.Input.Domain() == "beego.me"
   143  	}).
   144  		AutoRouter(&TestController{})
   145  	AddNamespace(ns)
   146  	BeeApp.Handlers.ServeHTTP(w, r)
   147  	if w.Code != 405 {
   148  		t.Errorf("TestNamespaceCond can't run get the result " + strconv.Itoa(w.Code))
   149  	}
   150  }
   151  
   152  func TestNamespaceInside(t *testing.T) {
   153  	r, _ := http.NewRequest("GET", "/v3/shop/order/123", nil)
   154  	w := httptest.NewRecorder()
   155  	ns := NewNamespace("/v3",
   156  		NSAutoRouter(&TestController{}),
   157  		NSNamespace("/shop",
   158  			NSGet("/order/:id", func(ctx *context.Context) {
   159  				ctx.Output.Body([]byte(ctx.Input.Param(":id")))
   160  			}),
   161  		),
   162  	)
   163  	AddNamespace(ns)
   164  	BeeApp.Handlers.ServeHTTP(w, r)
   165  	if w.Body.String() != "123" {
   166  		t.Errorf("TestNamespaceInside can't run, get the response is " + w.Body.String())
   167  	}
   168  }