You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

201 lines
4.8 KiB

  1. /* eslint-env mocha */
  2. import expect from "expect"
  3. import { fromJS } from "immutable"
  4. import { parameterValues, contentTypeValues, operationScheme } from "corePlugins/spec/selectors"
  5. describe("spec plugin - selectors", function(){
  6. describe("parameterValue", function(){
  7. it("should return Map({}) if no path found", function(){
  8. // Given
  9. const spec = fromJS({ })
  10. // When
  11. let paramValues = parameterValues(spec, ["/one", "get"])
  12. // Then
  13. expect(paramValues.toJS()).toEqual({})
  14. })
  15. it("should return a hash of [parameterName]: value", function(){
  16. // Given
  17. const spec = fromJS({
  18. resolved: {
  19. paths: {
  20. "/one": {
  21. get: {
  22. parameters: [
  23. { name: "one", value: 1},
  24. { name: "two", value: "duos"}
  25. ]
  26. }
  27. }
  28. }
  29. }
  30. })
  31. // When
  32. let paramValues = parameterValues(spec, ["/one", "get"])
  33. // Then
  34. expect(paramValues.toJS()).toEqual({
  35. one: 1,
  36. two: "duos"
  37. })
  38. })
  39. })
  40. describe("contentTypeValues", function(){
  41. it("should return { requestContentType, responseContentType } from an operation", function(){
  42. // Given
  43. let state = fromJS({
  44. resolved: {
  45. paths: {
  46. "/one": {
  47. get: {
  48. "consumes_value": "one",
  49. "produces_value": "two"
  50. }
  51. }
  52. }
  53. }
  54. })
  55. // When
  56. let contentTypes = contentTypeValues(state, [ "/one", "get" ])
  57. // Then
  58. expect(contentTypes.toJS()).toEqual({
  59. requestContentType: "one",
  60. responseContentType: "two"
  61. })
  62. })
  63. it("should prioritize consumes value first from an operation", function(){
  64. // Given
  65. let state = fromJS({
  66. resolved: {
  67. paths: {
  68. "/one": {
  69. get: {
  70. "consumes_value": "one",
  71. "parameters": [{
  72. "type": "file"
  73. }],
  74. }
  75. }
  76. }
  77. }
  78. })
  79. // When
  80. let contentTypes = contentTypeValues(state, [ "/one", "get" ])
  81. // Then
  82. expect(contentTypes.toJS().requestContentType).toEqual("one")
  83. })
  84. it("should fallback to multipart/form-data if there is no consumes value but there is a file parameter", function(){
  85. // Given
  86. let state = fromJS({
  87. resolved: {
  88. paths: {
  89. "/one": {
  90. get: {
  91. "parameters": [{
  92. "type": "file"
  93. }],
  94. }
  95. }
  96. }
  97. }
  98. })
  99. // When
  100. let contentTypes = contentTypeValues(state, [ "/one", "get" ])
  101. // Then
  102. expect(contentTypes.toJS().requestContentType).toEqual("multipart/form-data")
  103. })
  104. it("should fallback to application/x-www-form-urlencoded if there is no consumes value, no file parameter, but there is a formData parameter", function(){
  105. // Given
  106. let state = fromJS({
  107. resolved: {
  108. paths: {
  109. "/one": {
  110. get: {
  111. "parameters": [{
  112. "type": "formData"
  113. }],
  114. }
  115. }
  116. }
  117. }
  118. })
  119. // When
  120. let contentTypes = contentTypeValues(state, [ "/one", "get" ])
  121. // Then
  122. expect(contentTypes.toJS().requestContentType).toEqual("application/x-www-form-urlencoded")
  123. })
  124. it("should be ok, if no operation found", function(){
  125. // Given
  126. let state = fromJS({ })
  127. // When
  128. let contentTypes = contentTypeValues(state, [ "/one", "get" ])
  129. // Then
  130. expect(contentTypes.toJS()).toEqual({
  131. requestContentType: undefined,
  132. responseContentType: undefined
  133. })
  134. })
  135. })
  136. describe("operationScheme", function(){
  137. it("should return the correct scheme for a remote spec that doesn't specify a scheme", function(){
  138. // Given
  139. let state = fromJS({
  140. url: "https://generator.swagger.io/api/swagger.json",
  141. resolved: {
  142. paths: {
  143. "/one": {
  144. get: {
  145. "consumes_value": "one",
  146. "produces_value": "two"
  147. }
  148. }
  149. }
  150. }
  151. })
  152. // When
  153. let scheme = operationScheme(state, ["/one"], "get")
  154. // Then
  155. expect(scheme).toEqual("https")
  156. })
  157. // it("should be ok, if no operation found", function(){
  158. // // Given
  159. // let state = fromJS({ })
  160. //
  161. // // When
  162. // let contentTypes = contentTypeValues(state, [ "/one", "get" ])
  163. // // Then
  164. // expect(contentTypes.toJS()).toEqual({
  165. // requestContentType: undefined,
  166. // responseContentType: undefined
  167. // })
  168. // })
  169. })
  170. })