|
@@ -1 +1,260 @@
|
|
package anonym_put
|
|
package anonym_put
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "net/http"
|
|
|
|
+ "net/url"
|
|
|
|
+ "os"
|
|
|
|
+ "strings"
|
|
|
|
+ "testing"
|
|
|
|
+
|
|
|
|
+ "git.p78su.freemyip.com/svi/gostore/pkg/mock/mock_service"
|
|
|
|
+ "git.p78su.freemyip.com/svi/gostore/pkg/mock/mock_store"
|
|
|
|
+ "git.p78su.freemyip.com/svi/gostore/pkg/mock/mock_web"
|
|
|
|
+ "github.com/gofiber/fiber/v2"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type tester struct {
|
|
|
|
+ t *testing.T
|
|
|
|
+ serv *mock_service.MockService
|
|
|
|
+ store *mock_store.MockStore
|
|
|
|
+ web *mock_web.MockWeb
|
|
|
|
+ router fiber.Router
|
|
|
|
+ anonPut *AnonymPut
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestAnonymPut(t *testing.T) {
|
|
|
|
+ fnClear := func() {
|
|
|
|
+ _ = os.RemoveAll("./store")
|
|
|
|
+ }
|
|
|
|
+ fnClear()
|
|
|
|
+ defer fnClear()
|
|
|
|
+ sf := &tester{
|
|
|
|
+ t: t,
|
|
|
|
+ }
|
|
|
|
+ sf.create()
|
|
|
|
+ sf.get()
|
|
|
|
+ sf.putCheck()
|
|
|
|
+ sf.post()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Помещение ключа в хранилище
|
|
|
|
+func (sf *tester) post() {
|
|
|
|
+ sf.t.Log("post")
|
|
|
|
+ sf.postBad1()
|
|
|
|
+ sf.postBad2()
|
|
|
|
+ sf.postBad3()
|
|
|
|
+ sf.postGood1()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (sf *tester) postGood1() {
|
|
|
|
+ sf.t.Log("postGood1")
|
|
|
|
+ app := fiber.New()
|
|
|
|
+ app.Post("/anonym/put", sf.anonPut.post)
|
|
|
|
+ formData := url.Values{
|
|
|
|
+ "key": {"key"},
|
|
|
|
+ "val": {"val"},
|
|
|
|
+ }
|
|
|
|
+ req, err := http.NewRequest("POST", "/anonym/put", strings.NewReader(formData.Encode()))
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postGood1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
+ resp, err := app.Test(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postGood1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Закрыто хранилище
|
|
|
|
+func (sf *tester) postBad3() {
|
|
|
|
+ sf.t.Log("postBad3")
|
|
|
|
+ app := fiber.New()
|
|
|
|
+ app.Post("/anonym/put", sf.anonPut.post)
|
|
|
|
+ formData := url.Values{
|
|
|
|
+ "key": {"key"},
|
|
|
|
+ }
|
|
|
|
+ req, err := http.NewRequest("POST", "/anonym/put", strings.NewReader(formData.Encode()))
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postBad3(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
+ mock_store.IsBad_ = true
|
|
|
|
+ resp, err := app.Test(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postBad3(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+ mock_store.IsBad_ = false
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Пустой ключ
|
|
|
|
+func (sf *tester) postBad2() {
|
|
|
|
+ sf.t.Log("postBad2")
|
|
|
|
+ app := fiber.New()
|
|
|
|
+ app.Post("/anonym/put", sf.anonPut.post)
|
|
|
|
+ formData := url.Values{
|
|
|
|
+ "key": {""},
|
|
|
|
+ }
|
|
|
|
+ req, err := http.NewRequest("POST", "/anonym/put", strings.NewReader(formData.Encode()))
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postBad2(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
+ resp, err := app.Test(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postBad2(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Кривой запрос
|
|
|
|
+func (sf *tester) postBad1() {
|
|
|
|
+ sf.t.Log("postBad1")
|
|
|
|
+ app := fiber.New()
|
|
|
|
+ app.Post("/anonym/put", sf.anonPut.post)
|
|
|
|
+ req, err := http.NewRequest("POST", "/anonym/put", nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postBad1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ resp, err := app.Test(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("postBad1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Онлайн проверка ключа
|
|
|
|
+func (sf *tester) putCheck() {
|
|
|
|
+ sf.t.Log("putCheck")
|
|
|
|
+ sf.putCheckBad1()
|
|
|
|
+ sf.putCheckBad2()
|
|
|
|
+ sf.putCheckGood1()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (sf *tester) putCheckGood1() {
|
|
|
|
+ sf.t.Log("putCheckGood1")
|
|
|
|
+ app := fiber.New()
|
|
|
|
+ formData := url.Values{
|
|
|
|
+ "key": {"key"},
|
|
|
|
+ }
|
|
|
|
+ app.Post("/anonym/put_check_key", sf.anonPut.putCheckKey)
|
|
|
|
+ req, err := http.NewRequest("POST", "/anonym/put_check_key", strings.NewReader(formData.Encode()))
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("putCheckGood1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
+ resp, err := app.Test(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("putCheckGood1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Пустой ключ в запросе
|
|
|
|
+func (sf *tester) putCheckBad2() {
|
|
|
|
+ sf.t.Log("putCheckBad2")
|
|
|
|
+ app := fiber.New()
|
|
|
|
+ formData := url.Values{
|
|
|
|
+ "key": {""},
|
|
|
|
+ }
|
|
|
|
+ app.Post("/anonym/put_check_key", sf.anonPut.putCheckKey)
|
|
|
|
+ req, err := http.NewRequest("POST", "/anonym/put_check_key", strings.NewReader(formData.Encode()))
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("putCheckBad2(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
+ resp, err := app.Test(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("putCheckBad2(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Нет нормального запроса
|
|
|
|
+func (sf *tester) putCheckBad1() {
|
|
|
|
+ sf.t.Log("putCheckBad1")
|
|
|
|
+ app := fiber.New()
|
|
|
|
+ app.Post("/anonym/put_check_key", sf.anonPut.putCheckKey)
|
|
|
|
+ req, err := http.NewRequest("POST", "/anonym/put_check_key", nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("putCheckBad1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ resp, err := app.Test(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("putCheckBad1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Получение страницы добавления
|
|
|
|
+func (sf *tester) get() {
|
|
|
|
+ sf.t.Log("get")
|
|
|
|
+ app := fiber.New()
|
|
|
|
+ app.Get("/anonym/put", sf.anonPut.get)
|
|
|
|
+ req, err := http.NewRequest("GET", "/anonym/put", nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("get(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ resp, err := app.Test(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("get(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ defer resp.Body.Close()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// create -- создает новый обработчик создания анонимного ключа в хранилище
|
|
|
|
+func (sf *tester) create() {
|
|
|
|
+ sf.t.Log("create")
|
|
|
|
+ sf.createBad1()
|
|
|
|
+ sf.createBad2()
|
|
|
|
+ sf.createGood1()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (sf *tester) createGood1() {
|
|
|
|
+ sf.t.Log("createGood1")
|
|
|
|
+ var err error
|
|
|
|
+ sf.anonPut, err = NewAnonymPut(sf.web, sf.router)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("createGood1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ if sf.anonPut == nil {
|
|
|
|
+ sf.t.Fatalf("createGood1(): anonPut==nil")
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Нет роутера
|
|
|
|
+func (sf *tester) createBad2() {
|
|
|
|
+ sf.t.Log("createBad2")
|
|
|
|
+ anonPut, err := NewAnonymPut(sf.web, nil)
|
|
|
|
+ if err == nil {
|
|
|
|
+ sf.t.Fatalf("createBad2(): err==nil")
|
|
|
|
+ }
|
|
|
|
+ if anonPut != nil {
|
|
|
|
+ sf.t.Fatalf("createBad2(): anonPut!=nil")
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// createBad1 -- нет объекта веб-сервера
|
|
|
|
+func (sf *tester) createBad1() {
|
|
|
|
+ sf.t.Log("createBad1")
|
|
|
|
+ sf.serv = mock_service.NewMockService()
|
|
|
|
+ var err error
|
|
|
|
+ sf.store, err = mock_store.NewMockStore(sf.serv)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("createBad1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ sf.serv.Store_ = sf.store
|
|
|
|
+ sf.web, err = mock_web.NewMockWeb(sf.serv)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sf.t.Fatalf("createBad1(): err=\n\t%v", err)
|
|
|
|
+ }
|
|
|
|
+ sf.router = sf.web.FiberApp().Group("/anonym")
|
|
|
|
+ anonPut, err := NewAnonymPut(nil, sf.router)
|
|
|
|
+ if err == nil {
|
|
|
|
+ sf.t.Fatalf("createBad1(): err==nil")
|
|
|
|
+ }
|
|
|
|
+ if anonPut != nil {
|
|
|
|
+ sf.t.Fatalf("createBad1(): anonPut!=nil")
|
|
|
|
+ }
|
|
|
|
+}
|