top

Utility functions to help writing tests for a Go-Json-Rest app

Go comes with net/http/httptest to help writing test for an http server. When this http server implements a JSON REST API, some basic checks end up to be always the same. This test package tries to save some typing by providing helpers for this particular use case.

package main

import (
        "github.com/ant0ine/go-json-rest/rest"
        "github.com/ant0ine/go-json-rest/rest/test"
        "testing"
)

func TestSimpleRequest(t *testing.T) {
        api := rest.NewApi()
        api.Use(rest.DefaultDevStack...)
        router, err := rest.MakeRouter(
                rest.Get("/r", func(w rest.ResponseWriter, r *rest.Request) {
                        w.WriteJson(map[string]string{"Id": "123"})
                }),
        )
        if err != nil {
               log.Fatal(err)
        }
        api.SetApp(router)
        recorded := test.RunRequest(t, api.MakeHandler(),
                test.MakeSimpleRequest("GET", "http://1.2.3.4/r", nil))
        recorded.CodeIs(200)
        recorded.ContentTypeIsJson()
}

Imported by 4 package(s)

  1. github.com/GregorioDiStefano/inventory/utils
  2. github.com/mendersoftware/deployments/utils/testing
  3. github.com/mendersoftware/go-lib-micro/testing
  4. github.com/mendersoftware/inventory/utils

Imported only in test by 141 package(s)

  1. bitbucket.org/hothai/trunkery
  2. github.com/8090boy/go-json-rest/rest
  3. github.com/AnuchitPrasertsang/contact-1
  4. github.com/DimShadoWWW/capturer
  5. github.com/ErebusBat/go-json-rest-middleware-jwt
  6. github.com/FraBle/simple-todo-list
  7. github.com/GregorioDiStefano/deviceadm
  8. github.com/GregorioDiStefano/deviceadm/requestid
  9. github.com/GregorioDiStefano/deviceadm/requestlog
  10. github.com/GregorioDiStefano/deviceauth
  11. github.com/GregorioDiStefano/deviceauth/requestid
  12. github.com/GregorioDiStefano/deviceauth/requestlog
  13. github.com/JekaMas/go-json-rest/rest
  14. github.com/KtorZ/go-json-rest/rest
  15. github.com/MaikuMori/go-json-rest/rest
  16. github.com/Pallat/api/handler
  17. github.com/Pallat/go_sim
  18. github.com/Pallat/reach
  19. github.com/STNS/STNS/stns
  20. github.com/SocialHarvestVendors/go-json-rest/rest
  21. github.com/Spokepoint/smtp-callback-verification
  22. github.com/StephanDollberg/go-json-rest-middleware-jwt
  23. github.com/Tovkal/go-json-rest/rest
  24. github.com/WnP/go-json-rest-middleware-jwt
  25. github.com/agrison/go-json-rest/rest
  26. github.com/aiyi/go-json-rest/rest
  27. github.com/alkasir/alkasir/pkg/shared/jwtmw
  28. github.com/angryfoxsu/Api.Go
  29. github.com/ant0ine/go-json-rest/rest
  30. github.com/benalexau/ibconnect.go/server
  31. github.com/benalexau/ibconnect/server
  32. github.com/bfallik/go-json-rest/rest
  33. github.com/canthefason/todo/todo
  34. github.com/chanshik/go-json-rest/rest
  35. github.com/clyphub/go-json-rest-middleware/redirect
  36. github.com/clyphub/go-json-rest/rest
  37. github.com/crunchydata/dnsbridge/deps/go-json-rest/rest
  38. github.com/cyberroadie/go-json-rest/rest
  39. github.com/dafang/go-json-rest/rest
  40. github.com/darvik80/go-json-rest/rest
  41. github.com/eAndrius/go-json-rest/rest
  42. github.com/eicca/smart-translate-server/api
  43. github.com/ghigt/go-json-rest-middleware-jwt
  44. github.com/gmaisto/go-json-rest/rest
  45. github.com/gwynantj/go-json-rest/rest
  46. github.com/hardPass/go-json-rest/rest
  47. github.com/huuzkee-foundation/go-json-rest/rest
  48. github.com/ikawaha/go-json-rest/rest
  49. github.com/iporsut/contact
  50. github.com/jadengore/go-json-rest-middleware-force-ssl
  51. github.com/jadengore/go-json-rest-middleware/redirect
  52. github.com/jadengore/go-json-rest/rest
  53. github.com/jburet/go-json-rest/rest
  54. github.com/jjenkins/go-json-rest/rest
  55. github.com/josharian/go-json-rest/rest
  56. github.com/joshheinrichs/geosource/server/api
  57. github.com/kimxilxyong/go-json-rest-middleware-jwt
  58. github.com/kristinn/go-json-rest/rest
  59. github.com/liangchenye/go-json-rest/rest
  60. github.com/lth2015/go-json-rest/rest
  61. github.com/maciejmrowiec/deviceauth
  62. github.com/maciejmrowiec/deviceauth/api/http
  63. github.com/maciejmrowiec/deviceauth/requestid
  64. github.com/maciejmrowiec/deviceauth/requestlog
  65. github.com/marconi/boltapi
  66. github.com/masahide/pyramid-scheme
  67. github.com/mattsch/go-json-rest/rest
  68. github.com/mclarkson/go-json-rest/rest
  69. github.com/mclarkson/obdi/external/ant0ine/go-json-rest/rest
  70. github.com/mendersoftware/deployments/resources/deployments/controller
  71. github.com/mendersoftware/deployments/resources/deployments/view
  72. github.com/mendersoftware/deployments/resources/images/controller
  73. github.com/mendersoftware/deployments/resources/images/view
  74. github.com/mendersoftware/deployments/resources/limits/controller
  75. github.com/mendersoftware/deployments/resources/tenants/controller
  76. github.com/mendersoftware/deployments/utils/restutil
  77. github.com/mendersoftware/deployments/utils/restutil/view
  78. github.com/mendersoftware/deviceauth/api/http
  79. github.com/mendersoftware/deviceauth/requestid
  80. github.com/mendersoftware/deviceauth/requestlog
  81. github.com/mendersoftware/go-lib-micro/context
  82. github.com/mendersoftware/go-lib-micro/customheader
  83. github.com/mendersoftware/go-lib-micro/identity
  84. github.com/mendersoftware/go-lib-micro/requestid
  85. github.com/mendersoftware/go-lib-micro/requestlog
  86. github.com/mendersoftware/go-lib-micro/routing
  87. github.com/mendersoftware/inventory
  88. github.com/mendersoftware/inventory/api/http
  89. github.com/mendersoftware/inventory/requestid
  90. github.com/mendersoftware/inventory/requestlog
  91. github.com/mhemmings/go-json-rest/rest
  92. github.com/miha-stopar/go-json-rest/rest
  93. github.com/msutter/go-json-rest/rest
  94. github.com/novalagung/go-json-rest-middleware-jwt
  95. github.com/novalagung/go-json-rest/rest
  96. github.com/pallat/api/handler
  97. github.com/pallat/go_sim
  98. github.com/pallat/reach
  99. github.com/pdxjohnny/go-json-rest-middleware-jwt
  100. github.com/pdxjohnny/hangouts-call-center/http/api
  101. github.com/pdxjohnny/numapp/user/api
  102. github.com/peergum/go-json-rest/rest
  103. github.com/philboltt/postit
  104. github.com/pyama86/STNS/stns
  105. github.com/pyama86/go-json-rest/rest
  106. github.com/q2/go-json-rest/rest
  107. github.com/qapi/go-json-rest/rest
  108. github.com/qmsk/close/control
  109. github.com/radioinmyhead/go-rest
  110. github.com/robstrong/go-json-rest/rest
  111. github.com/roofimon/contact_2nd_try
  112. github.com/roofimon/contact_3rd_try
  113. github.com/roofimon/reach
  114. github.com/sebest/go-json-rest/rest
  115. github.com/sioncheng/go-json-rest/rest
  116. github.com/songshusen/go-json-rest/rest
  117. github.com/stephandollberg/go-json-rest-middleware-jwt
  118. github.com/theclapp/go-json-rest/rest
  119. github.com/tomjowitt/go-json-rest/rest
  120. github.com/ulule/limiter
  121. github.com/unravelin/go-json-rest/rest
  122. github.com/urakozz/go-json-rest-middleware-jwt
  123. github.com/valorbreak/go-json-rest/rest
  124. github.com/veqryn/go-json-rest/rest
  125. github.com/wallarelvo/backfire/backfire
  126. github.com/widnyana/go-json-rest/rest
  127. github.com/wingyplus/diameter_spike/omr_diameter
  128. github.com/wingyplus/go-json-rest/rest
  129. github.com/wppurking/limiter
  130. github.com/wuyingsong/go-json-rest/rest
  131. github.com/yangmls/vcron/rest
  132. github.com/yannk/go-json-rest/rest
  133. github.com/yetis-br/travelPlanning
  134. github.com/youngtrips/go-json-rest/rest
  135. github.com/zaybiz/go-json-rest/rest
  136. github.com/zupa-hu/go-json-rest/rest
  137. github.com/zx9597446/go-json-rest/rest
  138. github.com/zx9597446/sigchecker
  139. github.com/zx9597446/smstoken
  140. gopkg.in/ant0ine/go-json-rest.v2/rest
  141. gopkg.in/ant0ine/go-json-rest.v3/rest