stringClass.test.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. import { stringUtils } from '../util/stringClass.js';
  3. // 测试获取服务器IP
  4. console.log('Server IP:', stringUtils.GetServerIP());
  5. // 创建模拟的Koa上下文对象
  6. const mockCtx = {
  7. request: {
  8. headers: {
  9. 'x-forwarded-for': '192.168.1.1, 10.0.0.1',
  10. 'x-real-ip': '192.168.1.2'
  11. },
  12. ip: '192.168.1.3',
  13. socket: {
  14. remoteAddress: '192.168.1.4'
  15. }
  16. },
  17. ip: '192.168.1.5'
  18. };
  19. // 测试不同场景的GetClientIP
  20. console.log('Client IP (with proxy headers):', stringUtils.GetClientIP(mockCtx));
  21. // 测试没有代理头的情况
  22. const mockCtxNoProxy = {
  23. request: {
  24. headers: {},
  25. ip: '192.168.2.1',
  26. socket: {
  27. remoteAddress: '192.168.2.2'
  28. }
  29. },
  30. ip: '192.168.2.3'
  31. };
  32. console.log('Client IP (no proxy headers):', stringUtils.GetClientIP(mockCtxNoProxy));
  33. // 测试IPv6格式
  34. const mockCtxIPv6 = {
  35. request: {
  36. headers: {
  37. 'x-forwarded-for': '::ffff:192.168.3.1'
  38. },
  39. ip: '::1',
  40. socket: {
  41. remoteAddress: '::ffff:192.168.3.2'
  42. }
  43. },
  44. ip: '::ffff:192.168.3.3'
  45. };
  46. console.log('Client IP (IPv6 format):', stringUtils.GetClientIP(mockCtxIPv6));