UrlCachingTests.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* eslint-disable
  2. no-path-concat,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const Client = require('./helpers/Client')
  14. const sinon = require('sinon')
  15. const ClsiApp = require('./helpers/ClsiApp')
  16. const host = 'localhost'
  17. const Server = {
  18. run() {
  19. const express = require('express')
  20. const app = express()
  21. const staticServer = express.static(__dirname + '/../fixtures/')
  22. app.get('/:random_id/*', (req, res, next) => {
  23. this.getFile(req.url)
  24. req.url = `/${req.params[0]}`
  25. return staticServer(req, res, next)
  26. })
  27. return app.listen(31415, host)
  28. },
  29. getFile() {},
  30. randomId() {
  31. return Math.random().toString(16).slice(2)
  32. }
  33. }
  34. Server.run()
  35. describe('Url Caching', function () {
  36. describe('Downloading an image for the first time', function () {
  37. before(function (done) {
  38. this.project_id = Client.randomId()
  39. this.file = `${Server.randomId()}/lion.png`
  40. this.request = {
  41. resources: [
  42. {
  43. path: 'main.tex',
  44. content: `\
  45. \\documentclass{article}
  46. \\usepackage{graphicx}
  47. \\begin{document}
  48. \\includegraphics{lion.png}
  49. \\end{document}\
  50. `
  51. },
  52. {
  53. path: 'lion.png',
  54. url: `http://${host}:31415/${this.file}`
  55. }
  56. ]
  57. }
  58. sinon.spy(Server, 'getFile')
  59. return ClsiApp.ensureRunning(() => {
  60. return Client.compile(
  61. this.project_id,
  62. this.request,
  63. (error, res, body) => {
  64. this.error = error
  65. this.res = res
  66. this.body = body
  67. return done()
  68. }
  69. )
  70. })
  71. })
  72. afterEach(function () {
  73. return Server.getFile.restore()
  74. })
  75. return it('should download the image', function () {
  76. return Server.getFile.calledWith(`/${this.file}`).should.equal(true)
  77. })
  78. })
  79. describe('When an image is in the cache and the last modified date is unchanged', function () {
  80. before(function (done) {
  81. this.project_id = Client.randomId()
  82. this.file = `${Server.randomId()}/lion.png`
  83. this.request = {
  84. resources: [
  85. {
  86. path: 'main.tex',
  87. content: `\
  88. \\documentclass{article}
  89. \\usepackage{graphicx}
  90. \\begin{document}
  91. \\includegraphics{lion.png}
  92. \\end{document}\
  93. `
  94. },
  95. (this.image_resource = {
  96. path: 'lion.png',
  97. url: `http://${host}:31415/${this.file}`,
  98. modified: Date.now()
  99. })
  100. ]
  101. }
  102. return Client.compile(
  103. this.project_id,
  104. this.request,
  105. (error, res, body) => {
  106. this.error = error
  107. this.res = res
  108. this.body = body
  109. sinon.spy(Server, 'getFile')
  110. return Client.compile(
  111. this.project_id,
  112. this.request,
  113. (error1, res1, body1) => {
  114. this.error = error1
  115. this.res = res1
  116. this.body = body1
  117. return done()
  118. }
  119. )
  120. }
  121. )
  122. })
  123. after(function () {
  124. return Server.getFile.restore()
  125. })
  126. return it('should not download the image again', function () {
  127. return Server.getFile.called.should.equal(false)
  128. })
  129. })
  130. describe('When an image is in the cache and the last modified date is advanced', function () {
  131. before(function (done) {
  132. this.project_id = Client.randomId()
  133. this.file = `${Server.randomId()}/lion.png`
  134. this.request = {
  135. resources: [
  136. {
  137. path: 'main.tex',
  138. content: `\
  139. \\documentclass{article}
  140. \\usepackage{graphicx}
  141. \\begin{document}
  142. \\includegraphics{lion.png}
  143. \\end{document}\
  144. `
  145. },
  146. (this.image_resource = {
  147. path: 'lion.png',
  148. url: `http://${host}:31415/${this.file}`,
  149. modified: (this.last_modified = Date.now())
  150. })
  151. ]
  152. }
  153. return Client.compile(
  154. this.project_id,
  155. this.request,
  156. (error, res, body) => {
  157. this.error = error
  158. this.res = res
  159. this.body = body
  160. sinon.spy(Server, 'getFile')
  161. this.image_resource.modified = new Date(this.last_modified + 3000)
  162. return Client.compile(
  163. this.project_id,
  164. this.request,
  165. (error1, res1, body1) => {
  166. this.error = error1
  167. this.res = res1
  168. this.body = body1
  169. return done()
  170. }
  171. )
  172. }
  173. )
  174. })
  175. afterEach(function () {
  176. return Server.getFile.restore()
  177. })
  178. return it('should download the image again', function () {
  179. return Server.getFile.called.should.equal(true)
  180. })
  181. })
  182. describe('When an image is in the cache and the last modified date is further in the past', function () {
  183. before(function (done) {
  184. this.project_id = Client.randomId()
  185. this.file = `${Server.randomId()}/lion.png`
  186. this.request = {
  187. resources: [
  188. {
  189. path: 'main.tex',
  190. content: `\
  191. \\documentclass{article}
  192. \\usepackage{graphicx}
  193. \\begin{document}
  194. \\includegraphics{lion.png}
  195. \\end{document}\
  196. `
  197. },
  198. (this.image_resource = {
  199. path: 'lion.png',
  200. url: `http://${host}:31415/${this.file}`,
  201. modified: (this.last_modified = Date.now())
  202. })
  203. ]
  204. }
  205. return Client.compile(
  206. this.project_id,
  207. this.request,
  208. (error, res, body) => {
  209. this.error = error
  210. this.res = res
  211. this.body = body
  212. sinon.spy(Server, 'getFile')
  213. this.image_resource.modified = new Date(this.last_modified - 3000)
  214. return Client.compile(
  215. this.project_id,
  216. this.request,
  217. (error1, res1, body1) => {
  218. this.error = error1
  219. this.res = res1
  220. this.body = body1
  221. return done()
  222. }
  223. )
  224. }
  225. )
  226. })
  227. afterEach(function () {
  228. return Server.getFile.restore()
  229. })
  230. return it('should not download the image again', function () {
  231. return Server.getFile.called.should.equal(false)
  232. })
  233. })
  234. describe('When an image is in the cache and the last modified date is not specified', function () {
  235. before(function (done) {
  236. this.project_id = Client.randomId()
  237. this.file = `${Server.randomId()}/lion.png`
  238. this.request = {
  239. resources: [
  240. {
  241. path: 'main.tex',
  242. content: `\
  243. \\documentclass{article}
  244. \\usepackage{graphicx}
  245. \\begin{document}
  246. \\includegraphics{lion.png}
  247. \\end{document}\
  248. `
  249. },
  250. (this.image_resource = {
  251. path: 'lion.png',
  252. url: `http://${host}:31415/${this.file}`,
  253. modified: (this.last_modified = Date.now())
  254. })
  255. ]
  256. }
  257. return Client.compile(
  258. this.project_id,
  259. this.request,
  260. (error, res, body) => {
  261. this.error = error
  262. this.res = res
  263. this.body = body
  264. sinon.spy(Server, 'getFile')
  265. delete this.image_resource.modified
  266. return Client.compile(
  267. this.project_id,
  268. this.request,
  269. (error1, res1, body1) => {
  270. this.error = error1
  271. this.res = res1
  272. this.body = body1
  273. return done()
  274. }
  275. )
  276. }
  277. )
  278. })
  279. afterEach(function () {
  280. return Server.getFile.restore()
  281. })
  282. return it('should download the image again', function () {
  283. return Server.getFile.called.should.equal(true)
  284. })
  285. })
  286. return describe('After clearing the cache', function () {
  287. before(function (done) {
  288. this.project_id = Client.randomId()
  289. this.file = `${Server.randomId()}/lion.png`
  290. this.request = {
  291. resources: [
  292. {
  293. path: 'main.tex',
  294. content: `\
  295. \\documentclass{article}
  296. \\usepackage{graphicx}
  297. \\begin{document}
  298. \\includegraphics{lion.png}
  299. \\end{document}\
  300. `
  301. },
  302. (this.image_resource = {
  303. path: 'lion.png',
  304. url: `http://${host}:31415/${this.file}`,
  305. modified: (this.last_modified = Date.now())
  306. })
  307. ]
  308. }
  309. return Client.compile(this.project_id, this.request, (error) => {
  310. if (error != null) {
  311. throw error
  312. }
  313. return Client.clearCache(this.project_id, (error, res, body) => {
  314. if (error != null) {
  315. throw error
  316. }
  317. sinon.spy(Server, 'getFile')
  318. return Client.compile(
  319. this.project_id,
  320. this.request,
  321. (error1, res1, body1) => {
  322. this.error = error1
  323. this.res = res1
  324. this.body = body1
  325. return done()
  326. }
  327. )
  328. })
  329. })
  330. })
  331. afterEach(function () {
  332. return Server.getFile.restore()
  333. })
  334. return it('should download the image again', function () {
  335. return Server.getFile.called.should.equal(true)
  336. })
  337. })
  338. })